Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Do I need to raise CollectionChanged?

Posted on 2010-11-30
6
Medium Priority
?
1,863 Views
Last Modified: 2012-08-14
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?

Thanks.
0
Comment
Question by:AIBMass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34238815
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.
0
 

Author Comment

by:AIBMass
ID: 34238936
I'm sure you are correct. However, I try:

... implements INotifyCollectionChanged

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

Then:

Me.MyColl = ServiceCall.Result
RaiseEvent CollectionChanged (Me.MyColl, _
    new(NotifyCOllectionChangedEventArgs(NotifyCollectionChangedAction.Reset))


and I still don't see my changed.

Did I set the event arguments correctly?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34239011
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):
MyColl.Clear()
MyColl.AddRange(ServiceCall.Result)

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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:AIBMass
ID: 34239115
Hmmm...MyColl does derive from Collection, it's an ObservableCollection - yes?

And AddRange does not exist for an ObservableCollection.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 34239201
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.
0
 

Author Closing Comment

by:AIBMass
ID: 34239269
Ultimately I had to use:

Me.MyColl.Clear
For each it as MyClass in ServiceCall.Result
  Me.MyColl.Add(it)
Next
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question