Overriding CollectionBase.Clear()

  • Thread starter Thread starter Jazper Manto
  • Start date Start date
J

Jazper Manto

hi

i wrote a class which is deverted from the CollectionBase.

now i tried to overwrite the Clear() method with the keyword "override". the
compiler says then that this is not possible because the base method is not
marked as virtual. however when i have a look at Clear method of the
CollectionBase class in the object browser, the method is marked as "virtual
new". why is it not possible to override this method? and why does the
compiler say, that this method is not virtual?

is my only possibility to shaddow the method in my class by "new"? this is
not my prefered solution because it's dangerous and i'd like to use
polymorphism.
does anybody got a better solution on that?

thanx in advance.
jazper manto
 
Jazper said:
hi

i wrote a class which is deverted from the CollectionBase.

now i tried to overwrite the Clear() method with the keyword "override". the
compiler says then that this is not possible because the base method is not
marked as virtual. however when i have a look at Clear method of the
CollectionBase class in the object browser, the method is marked as "virtual
new". why is it not possible to override this method? and why does the
compiler say, that this method is not virtual?

is my only possibility to shaddow the method in my class by "new"? this is
not my prefered solution because it's dangerous and i'd like to use
polymorphism.
does anybody got a better solution on that?

thanx in advance.
jazper manto

Hi jazper,

I think the object browser is wrong or theres a bug in it. I looked at
collectionbase using .net reflector and found there is not a virtual
clear(). I would try doing something like this instead:

public new void Clear()
{
//your code here...
base.Clear();
}
 
Jazper,
I've noticed that the OB (Object Browser) suggests that methods that are
implemented as part of an interface are virtual, when in fact they are not.
I suspect its just a quirk of how OB, as VB's OB normally reports them as
"Overridable NotOverridable"

You should be able to override CollectionBase.OnClear and/or
CollectionBase.OnClearComplete to handle any special processing during the
CollectionBase.Clear behavior.

NOTE: Be certain to read the remarks in the online help before overriding
CollectionBase.OnClear and/or CollectionBase.OnClearComplete...

I suspect that CollectionBase.Clear itself is not overridable so as to
ensure that CollectionBase.Clear does indeed clear the collection...

--
Hope this helps
Jay
T.S. Bradley - http://www.tsbradley.net


| hi
|
| i wrote a class which is deverted from the CollectionBase.
|
| now i tried to overwrite the Clear() method with the keyword "override".
the
| compiler says then that this is not possible because the base method is
not
| marked as virtual. however when i have a look at Clear method of the
| CollectionBase class in the object browser, the method is marked as
"virtual
| new". why is it not possible to override this method? and why does the
| compiler say, that this method is not virtual?
|
| is my only possibility to shaddow the method in my class by "new"? this is
| not my prefered solution because it's dangerous and i'd like to use
| polymorphism.
| does anybody got a better solution on that?
|
| thanx in advance.
| jazper manto
|
|
 
hi jay
thanx for your time.
You should be able to override CollectionBase.OnClear and/or
CollectionBase.OnClearComplete to handle any special processing during the
CollectionBase.Clear behavior.

that's a good one. its a proper workaround and fixes my problem.
thnx. jazper
 
hi rob
thanx for your time to answer my request.
I think the object browser is wrong or theres a bug in it. I looked at
collectionbase using .net reflector and found there is not a virtual
clear(). I would try doing something like this instead:

i found on the ILDASM on mscorelib.dll for the clear() method the following:
..method public hidebysig newslot virtual final
instance void Clear() cil managed
it is marked as virtual but also as final. does final mean not overridable
in IL?
public new void Clear()
{
//your code here...
base.Clear();
}
would be a solution, but when i use polymorphism. Ex. writing a method which
getting an instance of a collectionBase. now when my instance of the
deverted collectionBase will be down casted and clear() would be called, my
class can not react on that. so thats pretty dangerous.

thanx for your answer
jazper
 
Back
Top