Custom Collection Types

A

Adam Dockter

Okay something has been driving me crazy.

I see in the MSDN documentation that ToolBar.ToolBarButtonCollection
implements the IList interface.

The I look at members in the ToolBars.Buttons collection like:
public void Add(ToolBarButton button);
public ToolBarButton this[int index];

which don't implement the interface at all. The interface forces Add to take
an object and the indexer to return an object.

I guess what I am getting at is can I make my own custom collection class
that doesn't strictly implement the IList interface but just has methods
with the names of the IList interface, and then will it still work in the
designer. And Why does the documentation say that collections like
ToolBarButtonCollection implement IList when they really don't? Or am I
missing something else?

thanks,
Adam dR.
 
R

Robby

You do not have to implement all of the interfaces properties and methods as
public members. The interface methods and properties could be declared with
private or friend scope so that you do not see them directly when working
with the ToolBarButtonCollection. Here they have used strongly-typed Add
method and Item properties as public to prevent users passing the wrong type
of object. You can cast a ToolBarButtonCollection to an IList and see that
all the properties and methods are there and callable because you have
access to the interface definition even thought they are not visible through
the ToolBarButtonCollection object.

Hope this helps

Robby
 
A

Adam Dockter

I Tried what you were saying about make some of the member private or friend
and there is no friend accessor level and when I try to make one of the
IList members internal or private I get the compiler error:

MyCollection.cs(27): 'MyCollection' does not implement interface member
'System.Collections.IList.this[int]'. 'MyCollection.this[int]' is either
static, not public, or has the wrong return type.

It requires me to make them public. Thanks for the suggestion, maybe I am
still doing something wrong. Any other ideas?
 
R

Robby

Do you have a code snippet?


Adam Dockter said:
I Tried what you were saying about make some of the member private or
friend
and there is no friend accessor level and when I try to make one of the
IList members internal or private I get the compiler error:

MyCollection.cs(27): 'MyCollection' does not implement interface member
'System.Collections.IList.this[int]'. 'MyCollection.this[int]' is either
static, not public, or has the wrong return type.

It requires me to make them public. Thanks for the suggestion, maybe I am
still doing something wrong. Any other ideas?



Adam Dockter said:
Okay something has been driving me crazy.

I see in the MSDN documentation that ToolBar.ToolBarButtonCollection
implements the IList interface.

The I look at members in the ToolBars.Buttons collection like:
public void Add(ToolBarButton button);
public ToolBarButton this[int index];

which don't implement the interface at all. The interface forces Add to take
an object and the indexer to return an object.

I guess what I am getting at is can I make my own custom collection class
that doesn't strictly implement the IList interface but just has methods
with the names of the IList interface, and then will it still work in the
designer. And Why does the documentation say that collections like
ToolBarButtonCollection implement IList when they really don't? Or am I
missing something else?

thanks,
Adam dR.
 
A

Adam Dockter

I think I have it figured out.

You can implement the IList member like this, with not accessor level and
full qualified back to the interface.

int System.Collections.IList.Add(object value){...}
object System.Collections.IList.this[int index]{...}

Then you can defien your own types to your new collection:

public int Add(MyObject value){...}
public object this[int index]{...}

thanks for your help.


Robby said:
Do you have a code snippet?


Adam Dockter said:
I Tried what you were saying about make some of the member private or
friend
and there is no friend accessor level and when I try to make one of the
IList members internal or private I get the compiler error:

MyCollection.cs(27): 'MyCollection' does not implement interface member
'System.Collections.IList.this[int]'. 'MyCollection.this[int]' is either
static, not public, or has the wrong return type.

It requires me to make them public. Thanks for the suggestion, maybe I am
still doing something wrong. Any other ideas?



Adam Dockter said:
Okay something has been driving me crazy.

I see in the MSDN documentation that ToolBar.ToolBarButtonCollection
implements the IList interface.

The I look at members in the ToolBars.Buttons collection like:
public void Add(ToolBarButton button);
public ToolBarButton this[int index];

which don't implement the interface at all. The interface forces Add to take
an object and the indexer to return an object.

I guess what I am getting at is can I make my own custom collection class
that doesn't strictly implement the IList interface but just has methods
with the names of the IList interface, and then will it still work in the
designer. And Why does the documentation say that collections like
ToolBarButtonCollection implement IList when they really don't? Or am I
missing something else?

thanks,
Adam dR.
 

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