|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Overriding CollectionBase.Clear()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 Manto wrote:
Show quote > hi Hi jazper,> > 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 > > 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(); } -- Rob Schieber 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 i found on the ILDASM on mscorelib.dll for the clear() method the following:> collectionbase using .net reflector and found there is not a virtual > clear(). I would try doing something like this instead: ..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? > would be a solution, but when i use polymorphism. Ex. writing a method which> public new void Clear() > { > //your code here... > base.Clear(); > } > 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 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... Show quote "Jazper Manto" <ejaz***@hotmail.com> wrote in message news:%23eomHrcyFHA.1132@TK2MSFTNGP10.phx.gbl... | 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 | | |
|||||||||||||||||||||||