Bug in ObservableCollection?

Published
Last Modified
2 minute read

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:

Public Class ItemBitCollection(Of TItem)
    Inherits ObservableCollection(Of ItemBit(Of TItem))


End Class

Public Class ItemBit(Of TItem)

    Private m_item As TItem

End 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:

Public Class ItemBitCollection(Of TItemBit)
    Inherits ObservableCollection(Of TItemBit)

End Class

Public MustInherit Class ItemBit(Of TItem)

    Private m_item As TItem

End Class

Public Class DefaultItemBit
    Inherits ItemBit(Of String)

End Class

And then pass that class in it does work:

Dim 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 

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

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.​

Graham & Brown Logo
Genus Breeding Ltd Logo
MacDonald Humfrey (Automation) Ltd. Logo
New Signature Logo
Sage Logo
Capita Secure Information Solutions Ltd Logo
Healthgrades Logo
Boxit Document Solutions Logo
Akaditi Logo
Slaughter and May Logo
Slicedbread Logo
Alignment Healthcare Logo
Lean SA Logo
Higher Education Statistics Agency Logo
Flowmaster (a Mentor Graphics Company) Logo
Cognizant Microsoft Business Group (MBG) Logo
ALS Life Sciences Logo
Deliotte Logo
Department of Work and Pensions (UK) Logo
Washington Department of Enterprise Services Logo
New Hampshire Supreme Court Logo
Nottingham County Council Logo
Washington Department of Transport Logo
Royal Air Force Logo
ProgramUtvikling Logo
MacDonald Humfrey (Automation) Ltd. Logo
Slicedbread Logo
Akaditi Logo
Workday Logo
Alignment Healthcare Logo