D
DippyDog
A long time ago, (yes, yes, in a galaxy yadda yadda yadda) I created a
class which inherits List(Of CustomObj) and I needed to recalculate
some things whenever an item is added to or removed from the list. My
first attempt was to override the .Add() and .Remove() methods, but
they are not declared as 'Overridable.' So I Shadowed them instead,
calling in each the same method from MyBase and then calling a
ListChanged() method of my own in which I can update calculations. I
found that I also needed to shadow several other methods (AddRange,
RemoveRange, RemoveAt, Clear, Insert, etc.) to be sure to know when
items were being added or removed. Now I find that I also need to
filter additions to prevent the same object from being added twice
(which, it turns out, one can do, resulting in a list with a count
greater than the number of individual objects listed). Shooting from
the hip, I might dive into the code and wrap all additive methods with
an "If Not Me.Contains(item)" before calling the shadowed MyBase
method. BUT, this all feels like a horrible sin against the D.R.Y.
principle. Where did I go wrong? Many thanks up front!
class which inherits List(Of CustomObj) and I needed to recalculate
some things whenever an item is added to or removed from the list. My
first attempt was to override the .Add() and .Remove() methods, but
they are not declared as 'Overridable.' So I Shadowed them instead,
calling in each the same method from MyBase and then calling a
ListChanged() method of my own in which I can update calculations. I
found that I also needed to shadow several other methods (AddRange,
RemoveRange, RemoveAt, Clear, Insert, etc.) to be sure to know when
items were being added or removed. Now I find that I also need to
filter additions to prevent the same object from being added twice
(which, it turns out, one can do, resulting in a list with a count
greater than the number of individual objects listed). Shooting from
the hip, I might dive into the code and wrap all additive methods with
an "If Not Me.Contains(item)" before calling the shadowed MyBase
method. BUT, this all feels like a horrible sin against the D.R.Y.
principle. Where did I go wrong? Many thanks up front!