Overriding (Shadowing) list's Add, AddRange, Remove, etc

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!
 
F

Family Tree Mike

I think the BindingList class may help you out some, though it sounds like
you have recreated it's logic. You can subscribe to an event when the list
changes, rather than overriding the add, remove, et al, methods.

I'm not sure I understand why your list of values needs to filter unique
values though.
 
G

Göran Andersson

DippyDog said:
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!

Encapsulate the list instead of inherit from it. That way you have full
control over the methods to implement, and you only have to implements
the ones that you need.

Public Class CustomObjectList

Private _list As List(Of CustomObject)

Public Sub New()
_list = New List(Of CustomObject)
End Sub

Public Sub Add(CustomObject obj)
_list.Add(obj)
' and whatever...
End Sub

Public Sub Remove(CustomObject obj)
_list.Remove(obj)
' and whatever...
End Sub

' and so on...

End Class
 

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