Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection of objects that use same interface 1

Status
Not open for further replies.

JontyMC

Programmer
Nov 26, 2001
1,276
GB
I have some objects that use the same interface. How can I create a typed collection of these objects (ie only objects that use the interface).

So, say the interface has a property called myProperty. Then I could access that property of a particular object in the list using myCollection.myProperty.

Cheers,

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
You need to create a class that derives from the CollectionBase Class

I have a point collection that I implemented like this:
Code:
public class PointCollection : CollectionBase
	{
		// Constructors
		public PointCollection(){}
		// Data
		public Point this[int index]
		{
			get
			{
				return((Point) List[index]);
			}
			set  
			{
				List[index] = value;
			}
		}
		// Methods
		public int Add(Point value)  
		{
			return(List.Add(value));
		}
		public int IndexOf(Point value)  
		{
			return(List.IndexOf(value));
		}
		public void Insert(int index, Point value)  
		{
			List.Insert(index,value);
		}
		public void Remove(Point value)  
		{
			List.Remove(value);
		}
		public bool Contains(Point value)
		{
			// If value is not of type Point, this will return false.
			return(List.Contains(value));
		}
}
 
Thanks, that worked a treat!

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
OK, I have my interface collection set up and I have instantiated it. How then do I set up an onremove handler from the class where I have instantiated it?:
Code:
InterfaceCollection ic = new InterfaceCollection
ic.OnRemove += new Event handler...????
I know I can put code in the "public void Remove(Point value)" bit, but I need to make a new event handler from a different class because the code needs to reference an object in that class.

Cheers,

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
JontyMC -
It might be better asking this in a new thread, as it looks like you've changed topics.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
just fire the OnRemove code from within the Remove method i posted above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top