Know when inherited collection is modified?

S

sstory

I have a class in vb.net that
Inherits Generic.List(Of myclass)

I want to have totals in this class that are static...i.e. not recalculated
every time. They should be calculated once and never again unless the
collection is modified, or the data in one of the objects in the collection
is changed. How can I do this?

Thanks,

Shane
 
L

Lloyd Sheen

sstory said:
I have a class in vb.net that
Inherits Generic.List(Of myclass)

I want to have totals in this class that are static...i.e. not recalculated
every time. They should be calculated once and never again unless the
collection is modified, or the data in one of the objects in the collection
is changed. How can I do this?

Thanks,

Shane
I think that one part of your question can be dealt with easily
(somewhat). Since you inherit the list you can override the methods
that modify the collection like Add/AddRange/Clear etc. You would have
to do this since there are no events thrown when the collection is modified.

When that happens you can recalc the totals.

To know when members within the collection are modified you would have
to have those items raise and event when a value you want to watch is
changed.

LS
 
S

sstory

OK. I am shadowing each method as overrides is not allowed. You are correct
this should solve problem1.

As far as events goes, I can't imagine how I could make an event in MyClass
that would fire in the containing collection.
How could that happen?

Thanks,

Shane
 
L

Lloyd Sheen

sstory said:
OK. I am shadowing each method as overrides is not allowed. You are correct
this should solve problem1.

As far as events goes, I can't imagine how I could make an event in MyClass
that would fire in the containing collection.
How could that happen?

Thanks,

Shane

Since you are now shadowing each collection modifier, you can now get a
list of the object being added. Create an interface which is just an
event. The event parameters should either give you information on what
was changed but since you already have a calc routine you most likely
only need to know about the change.

Then for each object added do an addhandler to an event handler which
will cause the calc routine to be called.

LS
 
S

sstory

Very elegant solution! I never thought of that. I think it will work fine.

Thanks!
 
S

sstory

Thanks! it worked great!
Lloyd Sheen said:
Since you are now shadowing each collection modifier, you can now get a
list of the object being added. Create an interface which is just an
event. The event parameters should either give you information on what
was changed but since you already have a calc routine you most likely only
need to know about the change.

Then for each object added do an addhandler to an event handler which will
cause the calc routine to be called.

LS
 

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