a·gen·tic a·gil·i·ty

Bug in ObservableCollection?

TL;DR; Explains issues using ObservableCollection with generic types in .NET, why errors occur, and discusses workarounds and limitations when working with custom classes.

Published on
2 minute read
Image
https://nkdagility.com/resources/L9aNTno7QM3
Subscribe

I seam to be having a little problem. Now, this may be me being stupid, but I can’t get an ObservableCollection to work if you pass it a generic type!

For example, consider the following code:

 1Public Class ItemBitCollection(Of TItem)
 2    Inherits ObservableCollection(Of ItemBit(Of TItem))
 3
 4
 5End Class
 6
 7Public Class ItemBit(Of TItem)
 8
 9    Private m_item As TItem
10
11End Class

Now, if you create an instance of ItemBitCollection you will see an error on the IDE regardless of wither you use a custom object type or a String type to initialise it:

Bug in ObservableCollection?

Why does this not work? What is ObservableCollection doing that maybe it should not? Time to get Reflector out…

After some looking at the source I am still none the wiser. Even if you dumb down your classes to the bare minimum you cant pass a generic type into an ObservableCollection.

This is a limitation if you ask me…

Annoying solution:

If you create a fixed class type:

 1Public Class ItemBitCollection(Of TItemBit)
 2    Inherits ObservableCollection(Of TItemBit)
 3
 4End Class
 5
 6Public MustInherit Class ItemBit(Of TItem)
 7
 8    Private m_item As TItem
 9
10End Class
11
12Public Class DefaultItemBit
13    Inherits ItemBit(Of String)
14
15End Class

And then pass that class in it does work:

1Dim o As New ItemBitCollection(Of DefaultItemBit)

Although this is a work around, it causes other problems in my code… Ahh well… worth a try…

Technorati Tags: .NET   WPF

Subscribe

Connect with Martin Hinshelwood

If you've made it this far, it's worth a conversation. Tell us where you are and what you're trying to change. Martin reads every inquiry and replies personally.

Start the conversation

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Epic Games Logo

Epic Games

Kongsberg Maritime Logo

Kongsberg Maritime

Illumina Logo

Illumina

Ericson Logo

Ericson

Akaditi Logo

Akaditi

Philips Logo

Philips

Emerson Process Management Logo

Emerson Process Management

Lockheed Martin Logo

Lockheed Martin

Jack Links Logo

Jack Links

ALS Life Sciences Logo

ALS Life Sciences

Trayport Logo

Trayport

Slicedbread Logo

Slicedbread

YearUp.org Logo

YearUp.org

MacDonald Humfrey (Automation) Ltd. Logo

MacDonald Humfrey (Automation) Ltd.

Big Data for Humans Logo

Big Data for Humans

SuperControl Logo

SuperControl

Washington Department of Transport Logo

Washington Department of Transport

Nottingham County Council Logo

Nottingham County Council

Ghana Police Service Logo

Ghana Police Service

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Royal Air Force Logo

Royal Air Force

Teleplan Logo

Teleplan

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Illumina Logo

Illumina

ProgramUtvikling Logo

ProgramUtvikling

Bistech Logo

Bistech

Slaughter and May Logo

Slaughter and May