I have a C# collection class that inherits from SortedList. I want to be
able to hide certain methods such as Add. How do I do that?

Re: Basic question about inheritance/hiding methods by Jon

Jon
Sun Mar 26 01:26:39 CST 2006

Dave <none@nowhere.com> wrote:
> I have a C# collection class that inherits from SortedList. I want to be
> able to hide certain methods such as Add. How do I do that?

You can't. That would break Liskov's Substitutability Principle.

If you want to expose only part of a class's public interface, you
should use composition/aggregation, rather than inheritance.

You *could* just override Add to throw an exception - but that would be
bad form, as anyone who received your object as a SortedList would
expect to be able to add to it.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: Basic question about inheritance/hiding methods by profdotnet

profdotnet
Sun Mar 26 01:40:47 CST 2006

Use New keyword in C# to hide inherited methods from base class.

Check out this link.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfnewoppg.asp


Dave wrote:

> I have a C# collection class that inherits from SortedList. I want to be
> able to hide certain methods such as Add. How do I do that?


Re: Basic question about inheritance/hiding methods by Jon

Jon
Sun Mar 26 01:51:51 CST 2006

profdotnet <post.epatra@gmail.com> wrote:
> Use New keyword in C# to hide inherited methods from base class.
>
> Check out this link.
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfnewoppg.asp

That doesn't *really* hide the inherited members though - it just
allows you to create new members with the same name which aren't
overrides of the "old" members. It's not the same as preventing the
"old" members from being available.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: Basic question about inheritance/hiding methods by Kevin

Kevin
Sun Mar 26 09:28:38 CST 2006

To elaborate on Jon's reply: Create a new class that does not *inherit*
SortedList, but *contains* a SortedList instance in it. You can expose the
attributes of SortedList via the new class, and not expose those which you
want to hide.

In addition, you may want to implement some of the interfaces that
SortedList implements, for a variety of reasons.

I recently had a similar problem when creating a Generic HistoryList
Collection. There is no such Collection in the Framework. A HistoryList is a
Collection which has a limited capacity, and can grow to that capacity, but
no larger. When it reaches Capacity, it removes items from the beginning of
the list to make room for new ones. In addition, it has a Position, which
indicates the current Position in the list, to allow for both undoing and
redoing. One can move the Position backwards and forwards in the list. When
one adds an item at a Position which is not the end, all items from that
point forward are removed.

I looked at a number of different Generic Lists, Collections, and
Interfaces, and determined that the Generic List was my best place to start.
I created the List as a protected member, and exposed the method, properties
etc., which were relevant while hiding others. I then added methods,
properties, etc., which provided the "HistoryList-specific" functionality
required. I also implemented the IEnumerable, ICollection, and IList
interfaces, to make it useful in a variety of situations which require
interfaces.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1e9052872ca85a3d98cfb1@msnews.microsoft.com...
> Dave <none@nowhere.com> wrote:
>> I have a C# collection class that inherits from SortedList. I want to be
>> able to hide certain methods such as Add. How do I do that?
>
> You can't. That would break Liskov's Substitutability Principle.
>
> If you want to expose only part of a class's public interface, you
> should use composition/aggregation, rather than inheritance.
>
> You *could* just override Add to throw an exception - but that would be
> bad form, as anyone who received your object as a SortedList would
> expect to be able to add to it.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



Re: Basic question about inheritance/hiding methods by Dave

Dave
Sun Mar 26 12:19:52 CST 2006

Thanks for the detailed replies.

Aggregation/composition will be the way to go. This collection needs to be
remotable anyway so I'm already providing methods on another class to do
things like Add.



Re: Basic question about inheritance/hiding methods by n4ixt

n4ixt
Mon Mar 27 06:28:52 CST 2006

You can't. Inheritance doesn't allow you to do that.

What you could do is write your own class that does not inherit, but does
have a SortedList inside it. You will then have to implement all of the
properties and methods of the SortedList that you want exposed, and simply
not implment the ones that you don't want to have, like Add. A lot more
work, but it would meet your goal.

The second thing you could do is implement your own Add method, overriding
the one in SortedList. Only instead of doing anything it simply pops up a
message that says "Add is not allowed in this class" (or throw an error or
something like that). Much quicker and easier to implement, it prevents the
Add from working in your class.

Robert


"Dave" <none@nowhere.com> wrote in message
news:%234ebZtGUGHA.196@TK2MSFTNGP10.phx.gbl...
>I have a C# collection class that inherits from SortedList. I want to be
>able to hide certain methods such as Add. How do I do that?
>
>



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Re: Basic question about inheritance/hiding methods by sloan

sloan
Mon Mar 27 14:37:11 CST 2006


The adapter pattern is along this lines.

http://www.dofactory.com/Patterns/PatternAdapter.aspx

This allows you to only provide the functionality you desire.

There is also the
http://www.dofactory.com/Patterns/PatternFacade.aspx
Fascase pattern.

The differences are subtle , but there exist.




"Dave" <none@nowhere.com> wrote in message
news:%234ebZtGUGHA.196@TK2MSFTNGP10.phx.gbl...
> I have a C# collection class that inherits from SortedList. I want to be
> able to hide certain methods such as Add. How do I do that?
>
>