ProgrammingVB.NET Developer / Desktop Developer

How are collections of the ObservableCollection type implemented and used in Visual Basic, in which cases they are preferable to other collections, and how to ensure correct notification of changes in elements for data binding?

Pass interviews with Hintsage AI assistant

Answer.

Background

In Visual Basic, with the development of the .NET platform, extended collections such as ObservableCollection(Of T) emerged. This collection is designed to notify the user interface (UI) of data changes, which is especially important when working with data binding — for example, in WPF or WinForms with MVVM support. Early constructs like List(Of T) or ArrayList did not support automatic change notifications and required manual logic reworking for display.

Problem

Standard collections do not notify the interface or subscribed objects about their own changes — adding, removing, or rearranging elements. As a result, UI controls (such as lists, tables) do not automatically update and require redrawing or additional method calls for updates. This manual synchronization leads to errors and complicates code logic.

Solution

ObservableCollection(Of T) implements the INotifyCollectionChanged interface, which allows the UI to automatically learn of any additions, deletions, or changes in elements. For full data updates when the objects in the collection change, it is recommended that the stored elements also implement the INotifyPropertyChanged interface.

Code example:

Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Item Implements INotifyPropertyChanged Private _name As String Public Property Name As String Get Return _name End Get Set(value As String) If _name <> value Then _name = value RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name")) End If End Set End Property Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged End Class Dim items As New ObservableCollection(Of Item)() AddHandler items.CollectionChanged, Sub(s, e) Console.WriteLine($"Action: {e.Action}") End Sub items.Add(New Item() With {.Name = "Test 1"}) items(0).Name = "Updated Name"

Key features:

  • The collection notifies subscribers about additions, deletions, moves, and clears.
  • When properties of objects inside the collection change, support for INotifyPropertyChanged is required for automatic synchronization with the UI.
  • Used for binding to UI templates in WPF, where changes are reflected without additional code.

Tricky questions.

Can ObservableCollection notify about changes in properties of elements if the element changes but the collection itself (composition, size) does not change?

No, by default ObservableCollection only tracks changes to the collection itself, not the properties of individual elements. To reflect changes in properties in the UI, objects must implement the INotifyPropertyChanged interface, and the UI binding must support this.

Will notifications about changes occur if the collection is modified outside the UI thread?

No, changes to the collection outside the main (UI) thread will lead to errors or incorrect behavior in some frameworks (e.g., WPF). For multi-threaded changes, you need to use Dispatcher.Invoke or similar methods to ensure synchronization occurs in the correct thread.

Can ObservableCollection be used for binding in WinForms, or does it only work in WPF?

ObservableCollection works in WinForms as well, however, in WinForms manual synchronization and data updating are required, as the standard binding of this platform does not always recognize collection events. In WPF, the implementation is maximally transparent.

Common mistakes and anti-patterns

  • Using ObservableCollection, but the elements within it do not implement INotifyPropertyChanged, hence the UI does not update when properties change.
  • Modifying the collection from the wrong (non-UI) thread.
  • Replacing the entire collection instead of updating its contents, which breaks UI binding.

Real-life example

Negative case

A programmer creates a list of products for the interface, uses a regular List(Of T), and after any addition or removal of items, manually calls update methods for the UI. Sometimes they forget to update the list or incorrectly recalculate indices when removing items.

Pros:

  • Easier to implement for very small projects.
  • No need to learn about events and interfaces.

Cons:

  • UI is often out of sync with actual data.
  • Many manual errors and unnecessary code.
  • Maintenance becomes more complex as the project grows.

Positive case

ObservableCollection(Of T) is implemented and objects support INotifyPropertyChanged. All actions — addition, deletion, modification — are automatically reflected in the interface through binding. The UI logic becomes more straightforward, with fewer manual updates needed.

Pros:

  • UI and data are always synchronized.
  • Minimal manual intervention for control elements.
  • Easy to maintain and scale.

Cons:

  • Initially requires building support for events and interfaces.
  • Needs to address threading synchronization issues in complex scenarios.