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! 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!
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Hrvoje Voda |
last post by:
How to put a specified dataset table into an array list ?
Hrcko
|
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,...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
| | |