Typed List

M

MuZZy

Hi,

How do i implement a typed IList class?

Visual Studio automatically generates template code for a IList, but for
example indexer is of type 'object', but i need it of type 'MyType'.

class MyList: IList
{
<...snap...>
public object this[int index]
{
get {return null;}
set {}
}
<...snap...>

}


If i just put 'MyType' instead of 'object' it will of course give an
error that IList.this[index] has a wrong return type.

But i know that somehow it's possible to do that, because a lot of typed
collections, like ToolBarButtonCollection have typed indexers and other
returning interface methods also return typed objects.

Any ideas?

Thank you,
MuZZy
 
M

MuZZy

And the same question about IEnumerator implementation - what should i
do so that "Current" would return a typed object of type "MyType", not
of type 'object'?

Thank you
MuZZy
 
J

Joanna Carter [TeamB]

"MuZZy" <[email protected]> a écrit dans le message de (e-mail address removed)...

| How do i implement a typed IList class?
|
| Visual Studio automatically generates template code for a IList, but for
| example indexer is of type 'object', but i need it of type 'MyType'.
|
| class MyList: IList
| {
| <...snap...>
| public object this[int index]
| {
| get {return null;}
| set {}
| }
| <...snap...>
|
| }
|
|
| If i just put 'MyType' instead of 'object' it will of course give an
| error that IList.this[index] has a wrong return type.
|
| But i know that somehow it's possible to do that, because a lot of typed
| collections, like ToolBarButtonCollection have typed indexers and other
| returning interface methods also return typed objects.

Unless you are using generics in .NET 2.0, you need to write your own
"IThingList" interface that specifies "Thing" as the type passed to and
retrieved from properties and methods. Simply coy the IList interface,
rename it to "IThingList" and change everywhere you see object to "Thing".

Simply implementing IList means that you can add and retrieve anything
to/from a list; even if you derived from IList you would still be able to
bypass the additional typed methods/properties and use the original object
ones.

Joanna
 
M

Markus Stoeger

MuZZy said:
And the same question about IEnumerator implementation - what should i
do so that "Current" would return a typed object of type "MyType", not
of type 'object'?

Lookup "explicit interface implementation". It allows you to "hide" the
untyped interface members and implement typed ones instead. Thats what
the collection classes do too.

interface IMyInterface {
void Function(object o);
}

class MyClass : IMyInterface {
// explicit implementation (hidden)
void IMyInterface.Function(object o) {
// ... do something with o
}

// own, typed implementation
public void Function(int o) {
// call the exlicitely implemented function
((IMyInterface)this).Function(o);
}
}

Now if you have an instance of MyClass you'll only see the typed
function "Function" with the int parameter. The untyped function is
hidden (.. or only accessible when you cast the object to IMyInterface).

hth,
Max
 
A

Andreas Mueller

Joanna said:
"MuZZy" <[email protected]> a écrit dans le message de (e-mail address removed)...

| How do i implement a typed IList class?
|
| Visual Studio automatically generates template code for a IList, but for
| example indexer is of type 'object', but i need it of type 'MyType'.
|
| class MyList: IList
| {
| <...snap...>
| public object this[int index]
| {
| get {return null;}
| set {}
| }
| <...snap...>
|
| }
|
|
| If i just put 'MyType' instead of 'object' it will of course give an
| error that IList.this[index] has a wrong return type.
|
| But i know that somehow it's possible to do that, because a lot of typed
| collections, like ToolBarButtonCollection have typed indexers and other
| returning interface methods also return typed objects.

Unless you are using generics in .NET 2.0, you need to write your own
"IThingList" interface that specifies "Thing" as the type passed to and
retrieved from properties and methods. Simply coy the IList interface,
rename it to "IThingList" and change everywhere you see object to "Thing".

Simply implementing IList means that you can add and retrieve anything
to/from a list; even if you derived from IList you would still be able to
bypass the additional typed methods/properties and use the original object
ones.

Joanna
Or simply derive your list from System.Collections.CollectionBase. See
http://tinyurl.com/5mjfx for an example.
With .NET 2.0, use List<T>.

HTH,
Andy
 
S

Stefan Simek

I'd just recommend - reverse the implementation so that the explicit
interface method calls the public method. In this special case you'll
avoid boxing/unboxing, but IMHO, the public implementation should be
primary and the explicit interface implementation should only adapt the
interface method signature to the actual implementation.

interface IMyInterface {
void Function(object o);
}

class MyClass : IMyInterface {
// explicit implementation (hidden)
void IMyInterface.Function(object o) {
// call the public typed implementation
Function((int) o);
}

// own, typed implementation
public void Function(int i) {
// ... do something with i
}
}

Just my 2c
Stefan
 
M

MuZZy

Markus said:
Lookup "explicit interface implementation". It allows you to "hide" the
untyped interface members and implement typed ones instead. Thats what
the collection classes do too.

interface IMyInterface {
void Function(object o);
}

class MyClass : IMyInterface {
// explicit implementation (hidden)
void IMyInterface.Function(object o) {
// ... do something with o
}

// own, typed implementation
public void Function(int o) {
// call the exlicitely implemented function
((IMyInterface)this).Function(o);
}
}

Now if you have an instance of MyClass you'll only see the typed
function "Function" with the int parameter. The untyped function is
hidden (.. or only accessible when you cast the object to IMyInterface).

hth,
Max


Well, that's teh problem - how do i use explicit implementation with
operator []?
 
J

Joanna Carter [TeamB]

"MuZZy" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Well, that's teh problem - how do i use explicit implementation with
| operator []?

IThing IThingList.this[int index]
{
get { return (IThing) this[index] }
set { this[index] = value }
}

Joanna
 
M

MuZZy

Joanna said:
"MuZZy" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Well, that's teh problem - how do i use explicit implementation with
| operator []?

IThing IThingList.this[int index]
{
get { return (IThing) this[index] }
set { this[index] = value }
}

Joanna

Thanks, it worked!
 

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