473,714 Members | 2,021 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 2141
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
10280
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
5975
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
2426
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
3598
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
8817
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
9322
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9037
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
7963
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6641
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4470
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
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.