hiding members of an interface?

G

Guest

I am confused:

Question 1:
the online help for the Stack class reads:
Public Class Stack
Implements ICollection, IEnumerable, ICloneable

However the members of ICollection do not show up as members of Stack class.
Neither if I cast a Stack to ICollection.
How can I do this in my own class?

Question 2:
I implement an interface. that creates the skeleton code in my class.
I change the created scope from Public to Private:
Public Class Class1(Of T)
Implements ICollection(Of T)

Private Sub Add(ByVal item As T) Implements
System.Collections.Generic.ICollection(Of T).Add

End Sub

Casting an object of this class to the implemented interface still lets me
use the private method from another assembly:
Dim x As New PowerLib.Class1(Of Integer)
CType(x, ICollection(Of Integer)).Add(2)

Imho narrowing the scope should always work, the Add() method should not be
accessible.


I want to implement my own generic collections which need parts of
ICollection.
thank you very much. herbert
 
J

Jon Skeet [C# MVP]

herbert said:
I am confused:

Question 1:
the online help for the Stack class reads:
Public Class Stack
Implements ICollection, IEnumerable, ICloneable

However the members of ICollection do not show up as members of Stack class.
Neither if I cast a Stack to ICollection.

If you cast a Stack to ICollection, you should certainly have access to
the members of ICollection.
How can I do this in my own class?

Well, Stack implements ICollection using what's called explicit
interface implementation in C#. I'm not sure of the equivalent
terminology in VB.NET.
Question 2:
I implement an interface. that creates the skeleton code in my class.
I change the created scope from Public to Private:
Public Class Class1(Of T)
Implements ICollection(Of T)

Private Sub Add(ByVal item As T) Implements
System.Collections.Generic.ICollection(Of T).Add

End Sub

Casting an object of this class to the implemented interface still lets me
use the private method from another assembly:
Dim x As New PowerLib.Class1(Of Integer)
CType(x, ICollection(Of Integer)).Add(2)

It looks like this is the equivalent of explicit interface
implementation - in other words, this may well be the answer to your
first question :)
Imho narrowing the scope should always work, the Add() method should not be
accessible.

In that case you wouldn't be implementing ICollection.
I want to implement my own generic collections which need parts of
ICollection.

If you're claiming to implement ICollection, then that's saying you're
prepared for someone to call Add on an instance. You can (in VB.NET,
not C#) provide an Add method to use in your class and a differently
named method to implement ICollection.Add, but you'll always have to
have *a* method which implements ICollection.Add.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top