| |
Collections are great. You can add and remove any objects without worrying about the size of the list. But what happens when you want to restrict what kind of objects go into the collection? You create a type-safe collection class. Why would you want to do this? If you are creating your own class, let's say a list of people objects to be processed by a database, you wouldn't want someone adding an XmlNode object into your collection. I'll outline some simple instructions on how to create your own type-safe collection using the System.Collections.CollectionBase class.
We'll start with some sample code and I'll explain the sections and what's important to remember when creating your own collection.
using System;
public class People
{
public string FirstName;
public string LastName;
}
public class PeopleCollection : CollectionBase
{
public void Add(People _people)
{
List.Add(_people);
}
public void Remove(int index)
{
if ((index > (Count - 1)) || (index < 0))
throw (new Exception("Index not for PeopleCollection valid!"));
List.RemoveAt(index);
}
public virtual People this[int index]
{
get
{
return (People) this.List[index];
}
}
}
|
Looks pretty simple, doesn't it?
People Class This is the object you want to create a collection for. In this example, I've created People class which just contains a first name and a last name. We're going to restrict our new collection to only accept People objects.
PeopleCollection Class First, let's look at the class signature. We'll notice that we're inheriting the CollectionBase class. "This base class is provided to make it easier for implementers to create a strongly typed custom collection. Implementers should extend this base class instead of creating their own" (MSDN Documentation). This is a "must do" when creating your own collection.
Add/Remove Methods In our new collection, we have the two basic commands everyone will want in their collection. Add will take a People object as a parameter and insert it into the List (which is part of the CollectionBase class). Since the Add method only accepts People objects, this is how the collection will restrict other objects from being added since the List object is not exposed. The Remove function takes an index as a parameter and removes the object associated with that index from the list. In our code, we've added some checking to make sure that the index exists and to throw an exception if the index is not valid. This remove method is only one way of doing it. It's also possible for a Remove method to be created based on first name or last name, or some other property of your class. Some people may even omit the remove method if they desire to do so.
Default Indexer Now that we can add and remove people object from our collection, we need a way to access the information. We implement a default indexer so that in our code, we can access the objects of our collection like this:
PeopleCollection myPeople = new PeopleCollection();
myPeople.Add(new People());
MessageBox.Show(myPeople[0].FirstName);
|
The last line of that code shows us an example of the indexer in use. It's getting the first name of the 4th person in our list (the indices start with 0). Again, just like the remove method, we can implement the indexer to return a People object based on first name (instead of index).
Other methods you can use Although this tutorial only shows you a collection with an add method, a remove method and a default indexer, you can add more methods to help you code. Some of the collections I've written provide me a method to access the last object in the collection or the first object in the collection. The code would be like this:
public People LastPerson
{
get
{
return (People) this.List[this.List.Count - 1];
}
}
|
We've now completed this tutorial. These are the simple steps for creating your own type-safe collection. Hope this tutorial helps!
Tips Don't forget to the "using System.Collections" line or you'll have to specify the full System.Collections.CollectionBase class name.
Its recommended that you put the post-fix "Collection" at the end of your collection name.
In your remove function, make sure the index exists. Links Devhood C# Message Forum - Feel free to ask questions to me or other Devhood members. http://www.devhood.com - Can't forget to mention this great resource. :-) - From MSDN.
Copyright © 2001 Andrew Ma.
|
|