473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Need explanation on creating a custom collection.

I want to create a custom collection class that is similar to something like
the System.Windows.Forms.Control.ControlCollection.

I notice that this collection implements IList, ICollection, IEnumrable, and
ICloneable. I have tried to create a similar class, but one thing that I
cannot seem to understand is that the ControlCollection provides overrides
to the interfaces with specific object types rather than the generic object
class. i.e., Add method accepts a Control type and not a generic Object
type as is specified in IList.

When I have tried to do the same, I cannot get the compiler to allow me to
specify a method with a more specific type as an override for the interface.
Can someone explain how to do this?

C# snippet example:

public class MyClass : IList
{
public virtual void Add(Control.Label MyLabel); // How can I implement
IList.Add with a specific type?

}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------
Nov 15 '05 #1
2 4489
Hi Ken,

You can do so inheriting from CollectionBase. CollectionBase is intended to
be used for this, it use an ArrayList internally to keep the items and you
provide the methods (Add,Delete, etc ) with the signature that you need.

Here is an example from one of my projects:
As you can see the class use the InnerList to keep the items, and as the
only way to access it is using your class's interface you can assure that
all the elements are of your selected type.
public class LocationPointCollection:CollectionBase

{

public LocationPoint Insert( int index, LocationPoint newelem )

{

this.InnerList.Insert( index, newelem);

return newelem;

}

public LocationPoint Add( LocationPoint newelem)

{

this.InnerList.Add( newelem);

return newelem;

}
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ken Varn" <va***@diebold.com> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
I want to create a custom collection class that is similar to something like the System.Windows.Forms.Control.ControlCollection.

I notice that this collection implements IList, ICollection, IEnumrable, and ICloneable. I have tried to create a similar class, but one thing that I
cannot seem to understand is that the ControlCollection provides overrides
to the interfaces with specific object types rather than the generic object class. i.e., Add method accepts a Control type and not a generic Object
type as is specified in IList.

When I have tried to do the same, I cannot get the compiler to allow me to
specify a method with a more specific type as an override for the interface. Can someone explain how to do this?

C# snippet example:

public class MyClass : IList
{
public virtual void Add(Control.Label MyLabel); // How can I implement IList.Add with a specific type?

}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------

Nov 15 '05 #2
Thanks! That clears things up a bit. I'll give that a try.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
news:Ow**************@tk2msftngp13.phx.gbl...
Hi Ken,

You can do so inheriting from CollectionBase. CollectionBase is intended to be used for this, it use an ArrayList internally to keep the items and you
provide the methods (Add,Delete, etc ) with the signature that you need.

Here is an example from one of my projects:
As you can see the class use the InnerList to keep the items, and as the
only way to access it is using your class's interface you can assure that
all the elements are of your selected type.
public class LocationPointCollection:CollectionBase

{

public LocationPoint Insert( int index, LocationPoint newelem )

{

this.InnerList.Insert( index, newelem);

return newelem;

}

public LocationPoint Add( LocationPoint newelem)

{

this.InnerList.Add( newelem);

return newelem;

}
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ken Varn" <va***@diebold.com> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
I want to create a custom collection class that is similar to something

like
the System.Windows.Forms.Control.ControlCollection.

I notice that this collection implements IList, ICollection, IEnumrable,

and
ICloneable. I have tried to create a similar class, but one thing that I cannot seem to understand is that the ControlCollection provides overrides to the interfaces with specific object types rather than the generic

object
class. i.e., Add method accepts a Control type and not a generic Object
type as is specified in IList.

When I have tried to do the same, I cannot get the compiler to allow me to specify a method with a more specific type as an override for the

interface.
Can someone explain how to do this?

C# snippet example:

public class MyClass : IList
{
public virtual void Add(Control.Label MyLabel); // How can I

implement
IList.Add with a specific type?

}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PK | last post by:
Hello, I am looking for help on the following. I'm trying to create a custom browser toolbar button that will do a few things. One that I'm trying to do at the moment is just simply return the...
3
by: Anthony Bouch | last post by:
Hi I've been reading using the XmlSerializer with custom collections. I've discovered that when serializing a custom collection (a class that implements ICollection, IList etc.) the...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
5
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
6
by: kbs | last post by:
Hi, I'm looking for some good examples that illustrate how to code a web service that exposes a custom collection so that the properties of the collection are accessible on the client without...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
6
by: MikeSwann | last post by:
Dear All, I am trying to decide on to create a collection object for a project that I am working on. I am fairly new to OOP so this may be on the basic side. I have looked on the groups, but...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
3
by: =?Utf-8?B?R2hpc3Rvcw==?= | last post by:
Hi all, We have a N-Tier framework and we now create a Web Site App to wotk with this architecture. I add reference from my libraries in the project and creating page and controls is very...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.