Know when inherited collection is modified?

  • Thread starter Thread starter sstory
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
Back
Top