Question about the CollectionBase class

C

craig

The .NET framework class CollectionBase has an overridable method called
OnSetComplete which takes three parameters:

1. int Index
2. object oldValue
3. object newValue

Apparently, the class calls this method right after any items in the
collection are modified in any way.

Am I correct in assuming that the oldValue represents the old value of the
item, and the newValue represents the new value of the item that replaces
oldValue??? Could these values be used to determine exactly how the item
with index Index has changed??

Thanks!
 
J

Jon Skeet [C# MVP]

craig said:
The .NET framework class CollectionBase has an overridable method called
OnSetComplete which takes three parameters:

1. int Index
2. object oldValue
3. object newValue

Apparently, the class calls this method right after any items in the
collection are modified in any way.
Yup.

Am I correct in assuming that the oldValue represents the old value of the
item, and the newValue represents the new value of the item that replaces
oldValue??? Could these values be used to determine exactly how the item
with index Index has changed??

Yes, that's right. Was there anything specific about it you wanted to
know about?
 
C

craig

Hey Jon! Thanks for the response!

Yes there actually is something specific that I woud like to know about
this.

I am using Rockford Lhotka's CSLA framework for .NET in my project. In this
framework, he develops a base collection class that can be derived from by a
business object developer in order to create a strongly-typed collection of
business objects.

In order to allow this class to be bindable using .NET data binding, he
implements the IBindingList interface, which includes a ListChanged event.
Pretty straight-forward stuff. So, in his class he includes the event code
as expected:

public event ListChangedEventHandler ListChanged;

virtual protected void OnListChanged(ListChangedEventArgs e)
{
if(ListChanged != null)
ListChanged(this, e);
}

Because his class is derived from CollectionBase, there are a series of
events in CollectionBase that can be used to raise the ListChanged event.
One of those events is the SetCompete event that I asked about in my
origional post. In order to raise a ListChanged event whenever the base
CollectionBase class raises its SetComplete event, Rocky overrides the
CollectionBase OnSetComplete method as follows:

override protected void OnSetComplete(int index, object oldValue, object
newValue)
{
OnListChanged( new ListChangedEventArgs(ListChangedType.ItemChanged,
Index);
}

This is great because someone using this collection can handle then handle
the ListChanged event, which is exactly what i want to do. I am using a
collection that is derived from Rocky's class asa child collection within a
parent object. I need to know if anything changes on any of the objects in
the child collection so that I can propagate those changes up through the
parent object and reflect them on my form. However, there is a problem...

If you look at the values that are passed to the event handler by the
ListChangedEventArgs object, you will notice that they do not include
oldValue and newValue, even though this information is made available by the
CollectionBase class. I need this information so that I can compare the two
objects to determine exactly what has changed between them. I can't do that
with nothing but an index.

But as I studied the ListChangedEventArgs type, I noticed that it does not
have fields for holding the oldValue and newValue values. I am not sure why
it was desinged this way. Why would a type designed to hold infomation
about a list changed event not be designed to hold all of the information
about that event??

So, I guess I am wondering how I can overcome this shortcoming. Should I
create my own ListCahnged event and my own ListChangedEventArgs type?

Well.......I hope I explained that clearly, Jon. Its probably a little more
than you were expecting!!!

If you have time to look it over and you have some comments, I would really
appreciate hearing from you. You have always been helpful in the past.

Thanks!
Craig
 
J

Jon Skeet [C# MVP]

So, I guess I am wondering how I can overcome this shortcoming. Should I
create my own ListCahnged event and my own ListChangedEventArgs type?

.... and override OnSetComplete? Yes, you could. That would seem a good
solution. Alternatively, you could modify the existing version of CSLA
- it looks like the source code is available. Check the licence first
though. You might also want to email Rocky to suggest that he makes the
change in his tree - if you want the feature, someone else is likely to
as well!
Well.......I hope I explained that clearly, Jon. Its probably a little more
than you were expecting!!!

Detail is always a good thing :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top