473,915 Members | 4,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ToolBarButtonCo llection have typed indexers and other
returning interface methods also return typed objects.

Any ideas?

Thank you,
MuZZy
Dec 25 '05 #1
8 2153
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 ToolBarButtonCo llection 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.Fu nction(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 ToolBarButtonCo llection 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.Collecti ons.CollectionB ase. 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.n et
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.Fu nction(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.Fu nction(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.Fu nction(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10293
by: Dot net work | last post by:
I thought I'd be clever and create my own strongly typed array list, because I wanted to be sure that when an object was added to the arraylist, it would *only* be an object of a class that I had made. But after I got it all working, I realised that the inheritance mechanism also allows access to the inherited Add methods, and these allow you to add objects of any type! I don't seem to have achieved what I tried to do.
20
5998
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
21
2451
by: Peter Bradley | last post by:
Hi all, This post is sort of tangentially related to my earlier posts on configuration files for DLLs. Does anyone know how to create typed DataSets using VS2005's new DataSet designer, but with the ability to configure the connection string via a config file? The designer seems to hard-code the connection string into the dataset itself, which just can't be right.
12
3611
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a gridview, for example, I loop through a datareader, populating an array list with instances of a custom class in the middle tier, and then send the array list up to the presentation layer and bind the gridview to it. If I use a typed dataset, I...
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9881
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
11066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5943
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4778
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.