471,306 Members | 1,432 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Typed List

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
Dec 25 '05 #1
8 2050
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
Dec 25 '05 #2
"MuZZy" <tn*@newsgroups.nospam> a écrit dans le message de news:
u4**************@TK2MSFTNGP09.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 25 '05 #3
MuZZy wrote:
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
Dec 25 '05 #4
Joanna Carter [TeamB] wrote:
"MuZZy" <tn*@newsgroups.nospam> a écrit dans le message de news:
u4**************@TK2MSFTNGP09.phx.gbl...

| 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
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Dec 25 '05 #5
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

Markus Stoeger wrote:
MuZZy wrote:
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

Dec 25 '05 #6
Markus Stoeger wrote:
MuZZy wrote:
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

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

Dec 25 '05 #7
"MuZZy" <tn*@newsgroups.nospam> a écrit dans le message de news:
OX**************@TK2MSFTNGP14.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 25 '05 #8
Joanna Carter [TeamB] wrote:
"MuZZy" <tn*@newsgroups.nospam> a écrit dans le message de news:
OX**************@TK2MSFTNGP14.phx.gbl...

| 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!
Dec 25 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.