• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2008
  • Last Modified:

Do I need to raise CollectionChanged?

I have an ObservableCollection that is set in Sub New via a service call.

Public Property MyColl as ObservableCollection(of MyClass)

Public Sub New()
MyColl = ServiceCall.Result
End Sub

I see the collection in my UI.

During processing, the service call may be repeated.

Public Sub SomeGoodReason
MyColl = ServiceCall.Result
End Sub

I know the collection has actually changed. Shouldn't the setting of the collection property to the result of the service call cause the Collection Changed event to fire because it's an ObservableCollection?

or does there have to be an explicit MyColl.Add (or Remove or Clear, etc)?

And if I have to fire the event myself, can you give me a VB snippet?

  • 3
  • 3
1 Solution
Carl TawnSystems and Integration DeveloperCommented:
No it won't fire because the collection itself hasn't changed. The thing that has changed is that the MyColl variable now points to a different collection.

You can't raise the event yourself because it is a Protected method of the class. You'll either need to add/remove a dummy item, or implement INotifyPropertyChanged and raise your own event.
AIBMassAuthor Commented:
I'm sure you are correct. However, I try:

... implements INotifyCollectionChanged

Public Event CollectionChanged (sender as Object, e as NotifyCollectionChangedEventArgs) implements....


Me.MyColl = ServiceCall.Result
RaiseEvent CollectionChanged (Me.MyColl, _

and I still don't see my changed.

Did I set the event arguments correctly?
Carl TawnSystems and Integration DeveloperCommented:
Implementing INotifyCollectionChanged probably isn't going to work if your class doesn't derive from Collection.

Instead of simply replacing your collection, it might be better to do (untested so might need tweaking):

Open in new window

That way you are actually modifying the current collection, rather than replacing it, which should make the normal event mechanism play properly.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

AIBMassAuthor Commented:
Hmmm...MyColl does derive from Collection, it's an ObservableCollection - yes?

And AddRange does not exist for an ObservableCollection.
Carl TawnSystems and Integration DeveloperCommented:
MyColl may be a collection, but the class that contains it isn't; and that is the one on which you are trying to implement the interface.

Try clearing the collection, then looping through teh new collection and adding the items to MyColl. That way you'll be able to see if it is firing properly.
AIBMassAuthor Commented:
Ultimately I had to use:

For each it as MyClass in ServiceCall.Result
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now