473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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!
Nov 17 '08 #1
2 5551
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.

"DippyDog" <di*******@gmail.comwrote in message
news:ef**********************************@r36g2000 prf.googlegroups.com...
>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!
Nov 18 '08 #2
DippyDog wrote:
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

--
Göran Andersson
_____
http://www.guffa.com
Nov 18 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
13
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
6
by: Brian Henry | last post by:
I was wondering how you guys would handle filling a list view with something like 10,000 items which each have 10 sub items on them... there has to be major speed issues with that right? Yet,...
1
by: Tariq | last post by:
Any thoughts on how a method on a base class can be prevented from being overriden or hidden? Basically the idea is that no matter what all derived classes should use the method in my base...
4
by: Phill. W | last post by:
Here's a knotty little problem. I have some nasty little controls that needs to behave in a non- windows-Standard way - don't ask why; it's a large application being converted from some older...
2
by: Richard Bysouth | last post by:
I'm sure this is something pretty trivial but can't seem to figure out how to do it. I'm using Generic Lists in quite a few places in my app and for some of them want to be able to create a...
1
by: Alex K. | last post by:
Hi all How do I catch an event when list item is deleted from ListBox? Is there such event? Thank you Alex
5
by: Roshawn | last post by:
Hi, Is it possible to override a property, function, or sub in a base class if the Overridable keyword isn't specified? For example: Public Class ExampleA Sub DoSomething() Dim str as...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.