IList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a collection class that inherits from CollectionBase. I want to
override the Contains method for the List property. I'm not sure how to do
that. Can anyone assist?

Thanks in advance,
Tom Costanza
 
Hello, Tom!

TC> I have a collection class that inherits from CollectionBase. I want to
TC> override the Contains method for the List property. I'm not sure how
TC> to do that. Can anyone assist?

Implement IList interface in your derived class

public interface I1

{

bool Contains();

}

public class B1 : I1

{

#region I1 Members

bool I1.Contains()

{

return false;

}

#endregion

}

public class C1 : B1, I1

{

bool I1.Contains()

{

return true;

}

}


--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Hi Vadym,

This looks like it adds a "Contains( )" method to MyDerivedClass, not
overrides the "MyDerivedClass.List.Contains( ) " method. Am I correct?
-Tom
 
You would have to re-implement IList in your derived class - because IList
is implemented explicitly you can't override it. Do your custom method for
Contains and make the others delegate to the base functionality.
 
TC> This looks like it adds a "Contains( )" method to MyDerivedClass, not
TC> overrides the "MyDerivedClass.List.Contains( ) " method. Am I correct?

you cannot override Contains since it is not virtual, you can only reimplement the IList.Contains().

Does this approach suit your needs? If not, why?
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Got it. Thanks all.

Clive Dixon said:
You would have to re-implement IList in your derived class - because IList
is implemented explicitly you can't override it. Do your custom method for
Contains and make the others delegate to the base functionality.
 
What if he made his class abstract?

Clive said:
You would have to re-implement IList in your derived class - because IList
is implemented explicitly you can't override it. Do your custom method for
Contains and make the others delegate to the base functionality.
 
As an addendum, I was hoping that you could delegate the other methods using
base.InnerList (you can't delegate using base.List as that is infinitely
recursive) e.g.

int Add(object value)
{
return base.InnerList.Add(value);
}

but if you look at the IList implementations in CollectionBase using
Reflector, they do different things to the coresponding InnerList methods
(in particular, the IList implementations tend to call several InnerList
methods rather than simply calling the corresponding method on InnerList).
So it's not that simple to delegate and reproduce the base class behaviour.
 
Back
Top