Bug in ObservableCollection?

Audience

Everyone

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:

image

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:   

Create a conversation around this article

Share on Facebook
Share on Twitter
Share on Linkdin

Read more

Martin Hinshelwood
In organizational development and team dynamics, Agile (as the Agile Manifesto delineates) and Scrum (as the Scrum Guide outlines) guide teams not by solving their problems but by illuminating the issues that demand attention. These frameworks aim to identify and spotlight the challenges within a team or organization’s processes, effectively …
Martin Hinshelwood
This week, I participated in a Scrum.org Webinar hosted by Sabrina Love (Scrum.org Product Owner) as well as my colleagues, Joanna Płaskonka, Ph.D. and Alex Ballarin to discuss the state of learning and how immersive learning is the future of training. You can watch the video below to hear what …
Martin Hinshelwood
For a long time now I have been searching for that perfect domain that epitomised the vision, the why, of what I am trying to achieve with my customers and the industry at large. Now I have found it in http://nkdagility.com
Martin Hinshelwood
At the MVP Summit I was appalled by the number of people who asked questions about new features for supporting hierarchical tasks! I shared a disgusted look with Peter Provost and we had a quick (and I mean really quick) conversation that resulted in this post. it really comes down …