Email me for updated C# and VB source code.
If you've been working with .NET for a while, I'm sure you've had the chance to create a collection before. Well, so have I. But, as I dug up old code two or three times, I figured there had to be an easier way to reuse this stuff. So, I figured I'd just make a template. Feel free to use this whenever you need it. I'll try to come up with other templates as I see the need for them or someone expresses the need. Here's how to use the template:
- Create new ObjectCollection.cs file, where Object is the name of the object you are making a collection of
- Copy source code listing (below)
- Paste source code into the ObjectCollection.cs file
- Search & replace “” with the desired namespace name
- Search & replace “” with the class name of the object you are making a collection of
For instance, if I have an object, MyCompany.MyApp.Domain.MyObject that I want to make a collection of, I'd replace “” with “MyCompany.MyApp.Domain” and “” with “MyObject”. This would create the MyCompany.MyApp.Domain.MyObjectCollection class.
I am only posting the C# code for this template. I will most likely post some VB code later. But, without further ado, the code...
using System;
using System.Collections;
namespace
{
///
/// Represents a collection of objects.
///
public class Collection : CollectionBase, IList
{
#region | Property Accessors |
///
/// Gets or sets the " /> object at the
/// specified index.
///
public this[int index]
{
get { return ()this.InnerList[index]; }
set { this.InnerList[index] = value; }
}
#endregion
#region | Methods [Public] |
///
/// Adds a object to the end of the
/// Collection.
///
///
/// The object to add to the end of
/// the Collection
///
///
/// The zero-based index at which the new element is inserted
///
public int Add( item)
{
return this.InnerList.Add(item);
}
///
/// Copies the elements of a array to
/// the end of the Collection.
///
///
/// An array of objects to add to the
/// end of the Collection - the array cannot be a
/// null reference (Nothing in Visual Basic)
///
///
/// is a null reference
/// (Nothing in Visual Basic)
///
public void AddRange([] items)
{
this.InnerList.AddRange(items);
}
///
/// Determines whether the specified
/// object is in the Collection.
///
///
/// The object to locate in the
/// Collection
///
/// true if value is found in the
/// Collection; otherwise, false
///
public bool Contains( item)
{
return this.InnerList.Contains(item);
}
///
/// Copies the entire Collection values to a
/// one-dimensional array of
/// objects, starting at the specified index of the target
/// array.
///
///
/// The one-dimensional array of
/// objects that is the destination of the elements copied
/// from Collection - array must use zero-based
/// indexing
///
///
/// The zero-based index in array at which copying begins.
///
///
/// is a null reference
/// (Nothing in Visual Basic)
/// </exception>
///
/// is less than zero
///
///
/// is multidimensional,
/// is equal to or greater than the
/// length of array, or the number of elements in the source
/// Collection is greater than the available space
/// from to the end of the
/// destination
/// < FONT>exception>
///
/// The type of the source Collection cannot be
/// cast automatically to the type of the destination
///
///
public void CopyTo([] array, int index)
{
this.InnerList.CopyTo(array, index);
}
///
/// Searches for the specified and
/// returns the zero-based index of the first occurrence
/// within the Collection.
///
///
/// The object to locate
///
///
/// Zero-based index of the first occurrence of value in the
/// Collection, if found; otherwise, -1
///
public int IndexOf( item)
{
return this.InnerList.IndexOf(item);
}
///
/// Inserts a object into the
/// Collection at the specified index.
///
///
/// The zero-based index at which value is inserted
///
///
/// The object to insert
///
public void Insert(int index, item)
{
this.InnerList.Insert(index, item);
}
///
/// Removes the first occurrence of a specific
/// from the Collection.
///
///
/// The object to remove from the
/// Collection
///
public void Remove( item)
{
this.InnerList.Remove(item);
}
#endregion
}
}
Please note that this code does not use generics. With slight modification it could be supported, but as-is, it does not. And, I had to expand the XML help in the supplied code a little so it would fit into the web view.