Implementing a Collection Using .NET
Although you can create a collection class from nothing, most collections class have some fundamental behaviors and characteristics. To give a skeleton of…
Common Behaviors of Collections#
Introduction#
All collections use ICollection as their base interface. It is located in the System.Collections namespace.
- Start Microsoft Visual Studio and create a new Windows Application named SoloMusicStore1
- To save the project, on the Standard toolbar, click the Save All button
- Accept all options and click Save
- To create a dialog box, on the main menu, click Project -> Add Windows Form...
- Set the name to ItemCategory and click Add
- Design the form as follows: Control Text Name Other Properties Label &Category: TextBox txtCategory Modifiers: Public Button OK btnOK DialogResult: OK Button Cancel btnCancel DialogResult: Cancel Form Property Value FormBorderStyle FixedDialog Text Category Editor StartPosition CenterScreen AcceptButton btnOK CancelButton btnCancel MaximizeBox False MinimizeBox False ShowInTaskbar False
- To create a dialog box, on the main menu, click Project -> Add Windows Form...
- Set the name to ItemType and click Add
- Design the form as follows: Control Text Name Other Properties Label &Item Type: TextBox txtItemType Modifiers: Public Button OK btnOK DialogResult: OK Button Cancel btnCancel DialogResult: Cancel Form Property Value FormBorderStyle FixedDialog Text Type Editor StartPosition CenterScreen AcceptButton btnOK CancelButton btnCancel MaximizeBox False MinimizeBox False ShowInTaskbar False
- To create a dialog box, in the Solution Explorer, right-click SoloMusic1 -> Add -> Windows Form...
- Set the name to ItemEditor and click Add
- Design the form as follows: Control Text Name Other Properties Label &Item #: TextBox txtItemNumber Label &Category: ComboBox cbxCategories Button New C&ategory... btnNewCategory Label &Item Type: ComboBox cbxItemTypes Button New &Item Type... btnNewItemType Label Item &Name: TextBox txtItemName Label &Unit Price: TextBox 0.00 txtUnitPrice TextAlign: Right Button Picture... btnPicture TextBox txtPicturePath PictureBox pbxPicturePath SizeMode: Zoom Button Create btnCreate Button Close btnClose DialogResult: Cancel OpenFileDialog (Name): dlgOpen Title: Select Item Picture DefaultExt: jpg Filter: JPEG Files (*.jpg,*.jpeg)|*.jpg|GIF Files (*.gif)|*.gif|Bitmap Files (*.bmp)|*.bmp|PNG Files (*.png)|*.png Form Property Value FormBorderStyle FixedDialog Text Musical Instrument Store - Item Editor StartPosition CenterScreen MaximizeBox False MinimizeBox False ShowInTaskbar False
- Double-click the New Category button and implement it as follows: private void btnNewCategory_Click(object sender, EventArgs e) { var editor = new ItemCategory(); if (editor.ShowDialog() == DialogResult.OK) { if (editor.txtCategory.Text.Length > 0) { var NewCategory = editor.txtCategory.Text; // Make sure the category is not yet in the list if (cbxCategories.Items.Contains(NewCategory)) MessageBox.Show(NewCategory + " is already in the list"); else { // Since this is a new category, add it to the combox box cbxCategories.Items.Add(NewCategory); // Just in case the user wanted to use this new category // select it cbxCategories.Text = NewCategory; } } } }
- Return to the Item Editor dialog box and double-click the New Item Type button
- Implement the event as follows: private void btnNewItemType_Click(object sender, EventArgs e) { var editor = new ItemType(); if (editor.ShowDialog() == DialogResult.OK) { if (editor.txtItemType.Text.Length > 0) { string NewType = editor.txtItemType.Text; // Make sure the type is not yet in the list if (cbxTypes.Items.Contains(NewType)) MessageBox.Show("The list already contains " + NewType); else { cbxTypes.Items.Add(NewType); cbxTypes.Text = NewType; } } } }
- Return to the Item Editor dialog box and double-click the Picture button
- Implement the event as follows: private void btnPicture_Click(object sender, EventArgs e) { if (dlgPicture.ShowDialog() == DialogResult.OK) { txtPicturePath.Text = dlgPicture.FileName; pbxStoreItem.Image = Image.FromFile(txtPicturePath.Text); } }
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type MusicStore.cs and press Enter twice (to display the form)
- Design the form as follows: Control Text Name Other Properties Label Item Category: ComboBox cbxCategories Label Items Type: ComboBox cbxTypes Label Available Items Button New Store Item... btnNewStoreItem ListView lvwStoreItems View: Details FullRowSelect: True GridLines: True Columns (Name) Text TextAlign Width colItemNumber Item # colItemName Item Name/Description 320 colUnitPrice Unit Price Right PictureBox pbxStoreItem SizeMode: Zoom Label Selected Items DataGridView Item # Label Description Columns (Name) HeaderText Width ReadOnly dgvItemNumber Item # 65 dgvItemName Item Name/Description 220 dgvUnitPrice Unit Price 70 dgvQuantity Qty 40 dgvSubTotal Sub-Total 80 True GroupBox Order Summary Label Items Total: TextBox 0.00 txtItemsTotal TextAlign: Right Label Tax Rate: TextBox 7.75 TextAlign: Right Label % Label Tax Amount: TextBox 0.00 txtTaxAmount TextAlign: Right Label Order Total: TextBox 0.00 txtOrderTotal TextAlign: Right Button Save btnSave Label Receipt #: TextBox 0.00 txtReceiptNumber Button &Open btnOpen Button New Order btnNewOrder Button Close btnClose
- Double-click the New Store Item button
- Implement the event as follows: private void btnNewStoreItem_Click(object sender, EventArgs e) { var editor = new ItemEditor(); // Create a random number to get it ready for // the user creating a new store item var rndNumber = new Random(); editor.txtItemNumber.Text = rndNumber.Next(100000, 999999).ToString(); editor.ShowDialog(); }
- Save all
Introduction to the ICollection Interface#
One of the primary pieces of information you should provide about the values in a database is the number of values that a list is (currently) holding. When creating a collection class, to prepare it to provide this valuable information, you can (should) implement an interface named ICollection. The ICollection interface is defined in the System.Collections namespace. The ICollection interface derives from the IEnumerable initerface:
public interface ICollection : IEnumerableThe generic equivalent of the ICollection interface is defined in the System.Collections.Generic namespace. The ICollection<> interface inherits from the IEnumerable<> and the IEnumerable interfaces:
public interface ICollection<T> : IEnumerable<T>,
IEnumerableTherefore, to start a collection class, you can creating implement one of these interfaces. Here is an example for the System.Collections.ICollection<> interface:
using System;
using System.Collections;
public class Collection<T> : ICollection<T>
{
}Thanks to the flexibility of arrays in the .NET Framework, you can create the items of the collection as an array and give it an initial size. Here is an example:
public class Collection<T> : ICollection<T>
{
private int size;
private T[] items;
public Collection()
{
size = 0;
items = new T[10];
}
}Implementing ICollection#
To assist you with keeping track of the number of items in a collection, the ICollection<T> interface is equipped with an integral property named Count, which you must implement. To do this, you can create a private member variable that will actually keep a count of the number of items. The Count property can then be used to communicate this information to the clients of the class. Here is an example:
using System;
using System.Collections;
public class Collection<T> : ICollection<T>
{
private int size;
public Collection()
{
size = 0;
}
public virtual int Count
{
get { return size; }
}
}The ICollection<> interface also allows its implementer to copy some of its items to an array. To provide this functionality, the interface is equipped with a method named CopyTo, which you must implement. The syntax of this method is:
void CopyTo(T[] array, int arrayIndex);This method takes two arguments. The first argument is the array that will receive the items. The second argument is the index of the item from where the copying operation will begin. Here is an example:
public class Collection<T> : ICollection<T>
{
private int size;
private T[] items;
public Collection()
{
size = 0;
items = new T[10];
}
public virtual int Count
{
get { return size; }
}
public void CopyTo(T[] array, int index)
{
T[] values = new T[size];
for (int i = 0; i < size; i++)
values[i] = items[i];
array = values;
}
}The System.Collections.ICollection.ICollection (and the System.Collections.Generic) interface extends the IEnumerable<> interface. This means that you should be able to use foreach in your ICollection<>-based class but you must create the functionality yourself, which is done by implementing the GetEnumerator() method. Because the ICollection<> interface inherits from IEnumerable<> that itself inherits from IEnumerable, you must implement two versions of the GetEnumerator() methods. As we saw in the previous lesson, their syntaxes are:
IEnumerator<T> GetEnumerator();
IEnumerator GetEnumerator();Even if you don't want to support this feature, you still must provide at least a skeleton for this method. Here is an example:
public class Collection<T> : ICollection<T>
{
private int size;
private T[] items;
public Collection()
{
size = 0;
items = new T[10];
}
public virtual int Count
{
get { return size; }
}
public void CopyTo(T[] array, int index)
{
T[] values = new T[size];
for (int i = 0; i < size; i++)
values[i] = items[i];
array = values;
}
public IEnumerator<T> GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
}Practical Learning: Creating a Class
- To create a new class, in the Class View, right-click SoloMusicStore1 -> Add -> Class...
- Set the Name to StoreItem and press Enter
- Change the file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SoloMusic1 { [Serializable] public class StoreItem { private string nbr; private string cat; private string tp; private string nm; private double prc; public string ItemNumber { get { return nbr; } set { nbr = value; } } public string Category { get { return cat; } set { cat = value; } } public string Type { get { return tp; } set { tp = value; } } public string ItemName { get { return nm; } set { nm = value; } } public double UnitPrice { get { return prc; } set { prc = value; } } public virtual bool Equals(StoreItem same) { if( (nbr == same.nbr) && (cat == same.cat) && (tp == same.tp) && (nm == same.nm) && (prc == same.prc)) return true; else return false; } public StoreItem() { nbr = "000000"; cat = "Accessories"; tp = "Accessories"; nm = "Unknown"; prc = 0.00; } public StoreItem(string itmNumber) { nbr = itmNumber; cat = "Accessories"; tp = "Accessories"; nm = "Unknown"; prc = 0.00; } public StoreItem(string itmNumber, string category, string type, string name, double price) { nbr = itmNumber; cat = category; tp = type; nm = name; prc = price; } public override string ToString() { return string.Format("Item #:\t{0}\nCategory:\t{1}\n" + "Item Type:\t{2}\nItem Name:\t{3}\n" + "Unit Price:\t{4}", nbr, cat, tp, nm, prc); } } }
- To create a new class, in the Class View, right-click SoloMusic1 -> Add -> Class...
- Set the Name to SoldItem and press Enter
- Change the file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SoloMusic1 { [Serializable] public class SoldItem { private string nbr; private string nm; private double prc; private int count; private double total; public string ItemNumber { get { return nbr; } set { nbr = value; } } public string ItemName { get { return nm; } set { nm = value; } } public double UnitPrice { get { return prc; } set { prc = value; } } public int Quantity { get { return count; } set { count = value; } } public double SubTotal { get { return total; } set { total = value; } } public virtual bool Equals(SoldItem same) { if( (nbr == same.nbr) && (nm == same.nm) && (prc == same.prc) && (count == same.count) && (total == same.total)) return true; else return false; } public SoldItem() { nbr = "000000"; nm = "Unknown"; prc = 0.00; count = 0; total = 0.00D; } public SoldItem(string itmNumber, string name, double price, int qty, double sub) { nbr = itmNumber; nm = name; prc = price; count = qty; total = sub; } public override string ToString() { return string.Format("Item #:\t{0}\nItem Name:\t{1}\n" + "Unit Price:\t{2}\nQuantity:\t{3}\nSub-Total:\t{4}", nbr, nm, prc, count, total); } } }
- Save all
The IList Interface#
Introduction#
While it provides the minimum functionality of a collection, the System.Collections.Generic.ICollection<> (and the System.Collections.ICollection) interface is not equipped to perform the regular operations of a collection class, such as adding, retrieving, or deleting items from a set. To assist you with creating a collection class as complete as possible, the .NET Framework provides the IList<> (and the IList) interface. The IList<> interface is defined in the System.Collections.Generic namespace and its non-generic equivalent of the same name is defined in the System.Collections namespace. The interface is equipped with the methods necessary to add, insert, delete, or retrieve items from a collection. Because the functionalities of these methods may not suit you, to use these features, you must create a class that implements them.
Implementing IList#
You can get the total number of elements in the collection via the Count property.
public interface IList : ICollection, IEnumerableThis System.Collections.Generic.IList<> interface is declared as follows:
public interface IList<T> : ICollection<T>,
IEnumerable<T>,
IEnumerableAs mentioned above, to create a collection, you can implement the IList<> interface. Here is an example:
using System;
using System.Collections.Generic;
public class Collection<T> : IList<T>
{
}This means that the IList<> interface extends the ICollection<>, the IEnumerable<>, and the IEnumerable interfaces. This also implies that you must implement the members of these parent interfaces, including the Count, the IsReadOnly properties, the CopyTo(), and the GetEnumerator() method. From what we learned with ICollection, here are examples of implementing these members for the System.Collections.IList interface:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
public Collection()
{
size = 0;
items = new T[10];
}
public void CopyTo(T[] array, int arrayIndex)
{
T[] values = new T[size];
for (int i = 0; i < size; i++)
values[i] = items[i];
array = values;
}
public int Count
{
get { return size; }
}
public IEnumerator<T> GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
}Practical Learning: Starting an IList Implementation
- To create a new class, in the Class View, right-click SoloMusicStore1 -> Add -> Class...
- Set the Name to StoreItems and press Enter
- Change the file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MusicStore1 { [Serializable] public class StoreItems <T> : IList<T> { } }
- In the Code Editor, right-click IList<>, position the mouse on Implement Interface, and click Implement Interface
- Based on issues we have already studied, change the document as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MusicStore1 { [Serializable] public class StoreItems<T> : IList<T> { // This field will be used to keeep a count of // the items as they are added or removed int counter; // This array holds the items of the collection private T[] objects; // The default constructor is used to initialize the collection public StoreItems() { counter = 0; objects = new T[10]; } #region IList<T> Members // This method is used to check whether the item passed as // argument exists in the collection. If so, it returns its index public int IndexOf(T item) { throw new NotImplementedException(); } // This method can be used to insert an item at // a certain position inside the collection public void Insert(int index, T item) { throw new NotImplementedException(); } // This method is used to delete the item positioned // at the index passed as argument public void RemoveAt(int index) { throw new NotImplementedException(); } public T this[int index] { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } #endregion #region ICollection<T> Members // This method is used to add a new item to the collection public void Add(T item) { throw new NotImplementedException(); } // This methods deletes all items from the collection public void Clear() { throw new NotImplementedException(); } // This method is used to find out whether the item // passed as argument exists in the collection public bool Contains(T item) { throw new NotImplementedException(); } // This method is used to copy the items of this collection // to another variable passed as argument public void CopyTo(T[] array, int arrayIndex) { T[] values = new T[arrayIndex]; for (int i = 0; i < counter; i++) values[i] = objects[i]; array = values; } // This read-only property holds // the total number of items in the collection public int Count { get { return counter ; } } // This property indicates whether the collection can receive new items public bool IsReadOnly { get { throw new NotImplementedException(); } } // This method first checks the existence of the item passed // as argument. If the item exists, the method deletes it public bool Remove(T item) { throw new NotImplementedException(); } #endregion #region IEnumerable<T> Members public IEnumerator<T> GetEnumerator() { int number = 0; while (number < Count) { yield return objects[number]; number++; } } #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { int number = 0; while (number < Count) { yield return objects[number]; number++; } } #endregion } }
- Save the file
Populating the Collection#
A Read-Only Collection#
Most collections are made to receive new values. If you want, you can create a list that cannot receive new values. To support this, the IList interface is equipped with the Boolean IsReadOnly property. If a list is read-only, it would prevent the clients from adding items to it. Here is an example of implementing the IsReadOnly property for the System.Collections.IList interface:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
public Collection()
{
size = 0;
items = new T[10];
}
public void CopyTo(T[] array, int arrayIndex)
{
T[] values = new T[size];
for (int i = 0; i < size; i++)
values[i] = items[i];
array = values;
}
public int Count
{
get { return size; }
}
public bool IsReadOnly
{
get { return false; }
}
public IEnumerator<T> GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
}Practical Learning: Setting the Read-Only Effect
- Change the StoreItems.cs file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MusicStore1 { [Serializable] public class StoreItems<T> : IList<T> { . . . No Change public bool IsReadOnly { get { return false ; } } . . . No Change } }
- Save the file
Adding an Item#
As it should be obvious, the primary operation to perform on a list is to populate it with at least one value. To support this, the IList<> interface is equipped with a method named Add. Its syntax is:
void Add(T item);This method takes one argument as the value to add to the collection. If your collection is an array, you can first check that there is still enough room in the list to add a new item. In reality, this is never an issue with the System.Collections.IList interface:
- If there is still room in the collection, the value would be added to the list
- If there is not enough room, the value would simply not be added
In Lesson 4, we saw that the Add() method can be defined as follows::
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
public int Add(double item)
{
this.items[this.size] = item;
this.size++;
return size;
}
}By default, an array has a fixed size. If you try adding an item in a position higher than the maximum number of items, the compiler would throw an IndexOutOfRangeException. Fortunately, the Array class is equipped with a method named Resize. This allows you to increase the size of an array when you judge it necessary. As a consequence, you can have an array as big as possible. Based on this, you can implement the Add() method as follows:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
private void CheckAndIncreaseIfNecessary()
{
if (size >= items.Length)
Array.Resize<T>(ref items, items.Length + 5);
}
public void Add(T value)
{
// Check whether there is still room in the array.
// If there is not enough room, then increase the capacity
CheckAndIncreaseIfNecessary();
// Add the item at the end
this.items[this.size] = value;
// Increase the current number of items
this.size++;
}
}Practical Learning: Adding an Item to the Collection
- Change the code of the StoreItems.cs file as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MusicStore1 { [Serializable] public class StoreItems<T> : IList<T> { // This field will be used to keeep a count of // the items as they are added or removed int counter; // This array holds the items of the collection private T[] objects; // The default constructor is used to initialize the collection public StoreItems() { counter = 0; objects = new T[10]; } // This method makes it possible to have a collection whose size can increase. // This method checks the size of the array. // If it is too small, its size gets increase. private void CheckToIncreaseSize() { if (counter >= objects.Length) Array.Resize<T>(ref objects, objects.Length + 5); } . . . No Change #region ICollection<T> Members // This method is used to add a new item to the collection public void Add(T item) { // Find out if the array is getting too small for the next item(s) // If it is, increase its size by 5 CheckToIncreaseSize(); // Add the item at the end this.objects[this.counter] = item; // Increase the current number of items this.counter++; } . . . No Change } }
- In the Class View, expand SoloMusicStore1 followed by SoloMusicStore1. Double-click ItemEditor
- In the top section of the file, add the following: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary;
- In the Solution Explorer, double-click ItemEditor.cs
- On the form, double-click the Create button
- Implement the event as follows: private void btnCreate_Click(object sender, EventArgs e) { FileStream stmStoreItem = null; var item = new StoreItem(); var items = new StoreItems<StoreItem>(); var bfmStoreItem = new BinaryFormatter(); // If this directory doesn't exist, create it Directory.CreateDirectory(@"C:\Solo Music Store"); // This is the file that holds the list of items var Filename = @"C:\Solo Music Store\StoreItems.slm"; // Create a random number that will be used to identify the item var rnd = new Random(); txtItemNumber.Text = rnd.Next(100000, 999999).ToString(); // Make sure the user had selected a category if (cbxCategories.Text.Length == 0) { MessageBox.Show("You must specify the item's category"); cbxCategories.Focus(); return; } // Make sure the user had selected a type if (cbxTypes.Text.Length == 0) { MessageBox.Show("You must specify the item's type"); cbxTypes.Focus(); return; } // Make sure the user had entered a name/description if (txtItemName.Text.Length == 0) { MessageBox.Show("You must enter the name (or a " + "short description) for the item"); txtItemName.Focus(); return; } // Make sure the user had typed a price for the item if (txtUnitPrice.Text.Length == 0) { MessageBox.Show("You must enter the price of the item"); txtUnitPrice.Focus(); return; } // Before saving the new item, find out if there was // already a file that holds the list of items // If that file exists, open it and store its items // in our StoreItems list if (File.Exists(Filename)) { stmStoreItem = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read); try { // Retrieve the list of items from file items = (StoreItems<StoreItem>)bfmStoreItem.Deserialize(stmStoreItem); } finally { stmStoreItem.Close(); } } // Create the music item item.ItemNumber = txtItemNumber.Text; item.Category = cbxCategories.Text; item.Type = cbxTypes.Text; item.ItemName = txtItemName.Text; item.UnitPrice = double.Parse(txtUnitPrice.Text); // Call the Add method of our collection class to add the item items.Add(item); // Save the list stmStoreItem = new FileStream(Filename, FileMode.Create, FileAccess.Write, FileShare.Write); try { bfmStoreItem.Serialize(stmStoreItem, items); if (txtPicturePath.Text.Length != 0) { FileInfo flePicture = new FileInfo(txtPicturePath.Text); flePicture.CopyTo(@"C:\Solo Music Store\" + txtItemNumber.Text + flePicture.Extension); } // After saving the item, reset the form txtItemNumber.Text = rnd.Next(100000, 999999).ToString(); cbxCategories.Text = ""; cbxTypes.Text = ""; txtItemName.Text = ""; txtUnitPrice.Text = "0.00"; txtPicturePath.Text = ""; pbxStoreItem.Image = null; } finally { stmStoreItem.Close(); } }
- Save the file
Inserting an Item#
When you call the Add() method, it adds the new value to the end of the list. Sometimes, you will want the new value to be inserted somewhere inside the list. To support this operation, both the System.Collections.IList and the System.Collections.Generic.IList interfaces provide a method named Insert. The syntax of the System.Collections.IList.Insert() method is:
void Insert(int index, object value);The IsSynchronized property indicates if access to the collection is thread-safe:
void Insert(int index, T value);This method takes two arguments. The second argument is the value that will be inserted into the list. The argument must hold a valid value. Because the System.Collections.IList.Insert() method takes an object value, if your collection is using a different type of value, you may have to cast it to object. The first argument is the index of the item that will precede the new one. As mentioned for the System.Collections.IList.Add() method, there are a few things you should know about this operation's success or lack of it:
- If the index argument holds a negative value or a value higher than the allowed number (for example if the list is a fixed array) of the items (depending on how you implement the method), the new value would not be added, the compiler would not throw an exception, and therefore nothing would let you know that the value was not added. If you want to find out whether the value was formally or actually inserted or not, you must create the functionality yourself
- If the value argument is not valid, again depending on how you structure your class, either the value would not be inserted or something else would go wrong. Fortunately, if the value argument is of the type of a class you created yourself, the compiler would produce an error such as letting you know that the argument is holding a value that is not conform to its property or member variable
The System.Collections.Generic.IList.Insert() method can be implemented as follows:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
private void CheckAndIncreaseIfNecessary()
{
if (size >= items.Length)
Array.Resize<T>(ref items, items.Length + 5);
}
public void Add(T value)
{
// Check whether there is still room in the array.
// If there is not enough room, then increase the capacity
CheckAndIncreaseIfNecessary();
// Add the item at the end
this.items[this.size] = value;
// Increase the current number of items
this.size++;
}
public void Insert(int index, T value)
{
// If the index is the same as the current number of items
// then call Add() and proceed as if we want to add a new item
if (index >= size)
{
Add(value);
return;
}
// If the index is positive but less than the current size,
// then you need to insert the item inside the collection.
if ((index >= 0) && (index < size))
{
// Firs, push each item one position up to create
// an empty space
for (int i = (size - 1); i > index - 1; i--)
items[i + 1] = items[i];
// Then put the new item in the indicated position
items[index] = value;
// Since the new item has been added, increase the
// current number of items
size++;
}
}When calling the IList<>.Insert() method, if you pass an invalid index, the compiler would throw an ArgumentOutOfRangeException.
Locating an Item in the Collection#
this Item of a Collection#
While using a list, various operations require that you know the object you are currently accessing. To provide this operation, IList and the IList<> interfaces are equipped with an indexed property named Item that you must implement. The Item property of the System.Collection.Generic.IList interface is declared as follows:
T this[int index] { get; set; }If you implement it right, this property can perform one of three operations:
- If you pass it an index that is the same number as the items in the collection and assign a value to it, it allows you to add an item at the end of the collection
- If you give it a positive index that is lower than the total number of items in the list and assign a value to it, it replaces the item at that position with the new value
- It you get the value at a positive index that is lower than the total number, it produces the item at that position
Here is an example of implementing the indexed property of the IList<> interface:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// The index property can be used to add an item to a collection,
// or to retrieve an item from the collection
public T this[int index]
{
get
{
return this.items[index];
}
set
{
this.items[index] = value;
}
}
}After creating this property, you can then access an item using its index and applying the [] operator on its instance. Practical Learning: Identifying this Item in the Collection
- In the Class View, click StoreItems<>
- In the lower part of the Class View, double-click this[int]
- Change the code of the property as follows: // This indexed property is used to represent an item based on its index public T this[int index] { // The get accessor is used to access the item at a specific index get { return objects[index] ; } // The set accessor can be used to replace the item at a specific index // or to add a new item to the end of the collection set { objects[index] = value ; } }
- In the Solution Explorer, double-click ItemEditor.cs
- Double-click an unoccupied area of the form to generate its Load event
- Implement the event as follows: private void ItemEditor_Load(object sender, EventArgs e) { // Since all values seem ready, prepare to process the item var item = new StoreItem(); var items = new StoreItems<StoreItem>(); var bfmStoreItem = new BinaryFormatter(); // This is the file that holds the list of items var Filename = @"C:\Solo Music Store\StoreItems.slm"; if (File.Exists(Filename)) { var stmStoreItem = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read); try { // Retrieve the list of items from file items = (StoreItems<StoreItem>)bfmStoreItem.Deserialize(stmStoreItem); // Display the categories in the combo box for (var i = 0; i < items.Count; i++) { item = (StoreItem)items[i]; if (!cbxCategories.Items.Contains(item.Category)) cbxCategories.Items.Add(item.Category); } // Display the items types in the combo box for (var i = 0; i < items.Count; i++) { item = (StoreItem)items[i]; if (!cbxTypes.Items.Contains(item.Type)) cbxTypes.Items.Add(item.Type); } } finally { stmStoreItem.Close(); } } else { // Create a random number that will be used // to identify the item var rnd = new Random(); txtItemNumber.Text = rnd.Next(100000, 999999).ToString(); // Make sure the user had selected a category cbxCategories.Text = ""; cbxTypes.Text = ""; } }
- Execute the application to test it
- Access the Item Editor dialog box and create a few items as follows (let the computer create the item numbers): Category Type Item Name Unit Price Picture Guitars Electric Gibson Les Paul Vintage Mahogany Electric Guitar 745.95 Picture Bass Electric 4-String Epiphone Thunderbird IV Bass 325.85 Picture Keyboards Synthesizers Alesis QS8.2 88 Key Synthesizer 825.50 Picture Guitars Acoustic Gretsch Guitars G100 Synchromatic Archtop Acoustic Guitar 595.95 Picture Drums Drum Set Pulse Pro 5-Piece Drum Set with Cymbals 395.95 Picture Keyboards Pianos Roland RD-700SX Digital Piano 2195.00 Accessories Cables Mogami Gold AES/EBU Interconnect Cable with Neutrik XLR 45.85 Guitars Acoustic-Electric Ibanez V Series V70CE Dreadnought Cutaway Acoustic-Electric Guitar 225.50 Guitars Electric Schecter C-1 Hellraiser Electric Guitar 650.00 Picture Keyboards Synthesizers Roland V Synth GT Elastic Audio Synthesizer Keyboard 2895.50 Picture Bass Electric 5-String Fender Jazz Bass 24 V 5-String Bass Guitar 825.50 Picture Guitars Electric Fender Standard Stratocaster Left-Handed Electric Guitar 425.85 Picture
- Close the forms and return to your programming environment
- In the Solution Explorer, right-click MusicStore.cs and click View Code
- Make the following changes: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace SoloMusic1 { public partial class MusicStore : Form { StoreItems<StoreItem> items; int iFilename; bool IsNewCustomerOrder; public MusicStore() { InitializeComponent(); } private void btnNewStoreItem_Click(object sender, EventArgs e) { var editor = new ItemEditor(); // Create a random number to get it ready for // the user creating a new store item var rnd = new Random(); editor.txtItemNumber.Text = rnd.Next(100000, 999999).ToString(); if ( editor.ShowDialog() == DialogResult.Cancel) LoadMusicStore() ; } internal void LoadMusicStore() { // Since all values seem ready, prepare to process the item items = new StoreItems<StoreItem>(); var bfmStoreItem = new BinaryFormatter(); // This is the file that holds the list of items var Filename = @"C:\Solo Music Store\StoreItems.slm"; if (File.Exists(Filename)) { FileStream stmStoreItem = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read); try { // Retrieve the list of items from file items = (StoreItems<StoreItem>)bfmStoreItem.Deserialize(stmStoreItem); // Display the categories in the combo box for (int i = 0; i < items.Count; i++) { StoreItem item = (StoreItem)items[i]; if (!cbxCategories.Items.Contains(item.Category)) cbxCategories.Items.Add(item.Category); } } finally { stmStoreItem.Close(); } } } } }
- In the Solution Explorer, double-click MusicStore.cs to open the form
- On the form, double-click the Item Category combo box
- Implement the SelectedIndexChanged event as follows: private void cbxCategories_SelectedIndexChanged(object sender, EventArgs e) { cbxTypes.Items.Clear(); cbxTypes.Text = ""; lvwStoreItems.Items.Clear(); pbxStoreItem.Image = null; // If the current store inventory is empty, don't do anything if (items.Count == 0) return; // Get the item selected in the combo box var strCategory = (string)cbxCategories.SelectedItem; // Before doing anything, remove everything from the Types combo box // This eliminates the possibility of adding the same item(s) that // would exist already in the combo box cbxTypes.Items.Clear(); // Check each item from the store inventory for (var i = 0; i < items.Count; i++) { // Get the current item from the store inventory var item = (StoreItem)items[i]; // If that item is the same as the one selected in the combo box... if (item.Category == strCategory) { // ... get ready to add its corresponding type // But check first that the type is not // already in the Types combo box // If it's not yet there, then add it if (!cbxTypes.Items.Contains(item.Type)) cbxTypes.Items.Add(item.Type); } } }
- Return to the Music Store form
- Double-click the Item Type combo box
- Implement the SelectedIndexChanged event as follows: private void cbxTypes_SelectedIndexChanged(object sender, EventArgs e) { cbxTypes.Text = ""; pbxStoreItem.Image = null; // If the current store inventory is empty, don't do anything if (items.Count == 0) return; // Get the item selected in the Categories combo box string strCategory = (string)cbxCategories.SelectedItem; // Get the item selected in theTypes combo box string strType = (string)cbxTypes.SelectedItem; // Empty the Available Items list view because // we are about to (re)populate it lvwStoreItems.Items.Clear(); // Check each item from the store inventory for (int i = 0; i < items.Count; i++) { // Get the current item from the inventory StoreItem item = (StoreItem)items[i]; // If the category of that item is the same as the one // selected in the combo box and the type of that item is // the same as the one selected in the Types combo box... if ((item.Category == strCategory) && (item.Type == strType)) { // ... then display it in the Available Items list view ListViewItem lviStoreItem = lvwStoreItems.Items.Add(item.ItemNumber); lviStoreItem.SubItems.Add(item.ItemName); lviStoreItem.SubItems.Add(item.UnitPrice.ToString("F")); } } }
- Return to the Music Store form
- On the form, double-click the Available Items list view
- Implement its SelectedIndexChanged event as follows: private void lvwStoreItems_SelectedIndexChanged(object sender, EventArgs e) { StoreItem item = new StoreItem(); if ((lvwStoreItems.SelectedItems.Count == 0) || (lvwStoreItems.SelectedItems.Count > 1)) return; string strItemNumber = lvwStoreItems.SelectedItems[0].SubItems[0].Text; for (int i = 0; i < items.Count; i++) { StoreItem itm = (StoreItem)items[i]; if (itm.ItemNumber == strItemNumber) item = itm; } // Make a list of the picture files string strDirectory = @"C:\Solo Music Store"; DirectoryInfo dirStoreItems = new DirectoryInfo(strDirectory); FileInfo[] PictureFiles = dirStoreItems.GetFiles("*.jpg"); // Look for a file that holds the same name as the item number foreach (FileInfo fle in PictureFiles) { // Get the name of the file without its extension string fwe = Path.GetFileNameWithoutExtension(fle.FullName); if (fwe == strItemNumber) pbxStoreItem.Image = Image.FromFile(strDirectory + "\\" + item.ItemNumber + ".jpg"); } }
- Return to the Music Store form and click the list view under Available Items if necessary
- In the Properties window, click the Events button and double-click DoubleClick
- Implement the event as follows: internal void CalculateOrder() { double itemsTotal = 0.00D, taxRate = 0.00D, taxAmount = 0.00D, orderTotal = 0.00D; // Check each row of the data grid view. // Get the sub-total values and add them for (int i = 0; i < dgvSelectedItems.Rows.Count - 1; i++) itemsTotal += double.Parse(dgvSelectedItems.Rows[i].Cells[4].Value.ToString()); // Display the total in the Items Total text box txtItemsTotal.Text = itemsTotal.ToString("F"); // Get the tax rate taxRate = double.Parse(txtTaxRate.Text); // Calculate the tax taxAmount = itemsTotal * taxRate / 100; // Add the tax amount to the items total to get the total of the order orderTotal = itemsTotal + taxAmount; // Display the amount collected for taxes txtTaxAmount.Text = taxAmount.ToString("F"); // Display the order total txtOrderTotal.Text = orderTotal.ToString("F"); } private void lvwStoreItems_DoubleClick(object sender, EventArgs e) { // Get a reference to the row that was double-clicked ListViewItem lviMusicItem = lvwStoreItems.SelectedItems[0]; // If no item exists or no row was double-selected, don't do anything if (lvwStoreItems.SelectedItems.Count == 0) return; // If a row was double-clicked, transfer its values to the data grid view dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[0].Value = lviMusicItem.Text; dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[1].Value = lviMusicItem.SubItems[1].Text; dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[2].Value = lviMusicItem.SubItems[2].Text; dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[3].Value = "1"; dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[4].Value = lviMusicItem.SubItems[2].Text; string[] recMusicItem = { lviMusicItem.Text, lviMusicItem.SubItems[1].Text, lviMusicItem.SubItems[2].Text, "1", lviMusicItem.SubItems[2].Text }; int r = dgvSelectedItems.Rows.Add(recMusicItem); // Reset the lastly added row to get it ready to receive another record dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[0].Value = ""; dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[1].Value = ""; dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[2].Value = ""; dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[3].Value = ""; dgvSelectedItems.Rows[dgvSelectedItems.Rows.Count - 1].Cells[4].Value = ""; CalculateOrder(); }
- Return to the Music Store form
- Click the data grid view under Selected Items
- In the Events section of the Properties window, double-click CellLeave
- Implement the event as follows: private void dgvSelectedItems_CellLeave(object sender, DataGridViewCellEventArgs e) { decimal unitPrice = 0.00M; int quantity = 0; decimal subTotal = 0.00M; // Find out if the user was changing the quantity value on the row that has focus if (e.ColumnIndex == 3) { // Get the unit price of the current row unitPrice = decimal.Parse(dgvSelectedItems.Rows[e.RowIndex].Cells[2].EditedFormattedValue.ToString()); // Get the quantity value of the current row quantity = int.Parse(dgvSelectedItems.Rows[e.RowIndex].Cells[3].EditedFormattedValue.ToString()); // Calculate the sub-total subTotal = unitPrice * quantity; // Display the sub-total on the current row dgvSelectedItems.Rows[e.RowIndex].Cells[4].Value = subTotal.ToString(); // Update the calculations CalculateOrder(); } }
- Return to the Music Store form
- On the form, click the Tax Rate text box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows: private void txtTaxRate_Leave(object sender, EventArgs e) { CalculateOrder(); }
- Return to the Music Store form
- Double-click the New Order button and implement its event as follows: private void btnNewCustomerOrder_Click(object sender, EventArgs e) { // We will store our files in the following folder var strDirectory = @"C:\Solo Music Store\Receipts"; var dirInfo = Directory.CreateDirectory(strDirectory); // Get the list of files, if any, from our directory var fleList = dirInfo.GetFiles(); var Filename = ""; // If there is no file in the directory, // then we will use 1000 as the first file name if (fleList.Length == 0) { iFilename = 1000; } else // If there was at least one file in the directory { // Get a reference to the last file var LastFile = fleList[fleList.Length - 1]; // Get the name of the last file without its extension var fwe = Path.GetFileNameWithoutExtension(LastFile.FullName); // Increment the name of the file by 1 iFilename = int.Parse(fwe) + 1; } // Update our global name of the file Filename = strDirectory + "\\" + iFilename.ToString() + ".its"; txtReceiptNumber.Text = iFilename.ToString(); // Reset the form cbxCategories.Text = ""; cbxTypes.Text = ""; lvwStoreItems.Items.Clear(); dgvSelectedItems.Rows.Clear(); txtItemsTotal.Text = "0.00"; txtTaxRate.Text = "7.75"; txtTaxAmount.Text = "0.00"; txtOrderTotal.Text = "0.00"; }
- Return to the Music Store form
- Double-click the Save button and implement the event as follows: private void btnSave_Click(object sender, EventArgs e) { if (dgvSelectedItems.Rows.Count == 0) { MessageBox.Show("There is no customer order to save."); return; } // We will store our files in the following folder var strSelectedItems = @"C:\Solo Music Store\Receipts\Selected Items"; var strTotals = @"C:\Solo Music Store\Receipts\Totals"; var dirSelectedItems = Directory.CreateDirectory(strSelectedItems); var dirTotals = Directory.CreateDirectory(strTotals); // Get the list of files, if any, from our directory FileInfo[] fleSelectedItems = dirSelectedItems.GetFiles(); var FilenameSelectedItem = ""; var FilenameTotal = ""; // If this is a new customer order, // get ready to create a name for the file if (IsNewCustomerOrder == true) { // If there is no file in the directory, // then we will use 1000 as the first file name if (fleSelectedItems.Length == 0) { iFilename = 1000; } else // If there was at least one file in the directory { // Get a reference to the last file var LastFile = fleSelectedItems[fleSelectedItems.Length - 1]; // Get the name of the last file without its extension var fwe = Path.GetFileNameWithoutExtension(LastFile.FullName); // Increment the name of the file by 1 iFilename = int.Parse(fwe) + 1; } // Update our global name of the file FilenameSelectedItem = strSelectedItems + "\\" + iFilename.ToString() + ".its"; FilenameTotal = strTotals + "\\" + iFilename.ToString() + ".ttl"; txtReceiptNumber.Text = iFilename.ToString(); IsNewCustomerOrder = false; } // If a cleaning order was already opened, we will simply update it else { FilenameSelectedItem = @"C:\Solo Music Store\Receipts\Selected Items\" + txtReceiptNumber.Text + ".its"; FilenameTotal = @"C:\Solo Music Store\Receipts\Totals\" + txtReceiptNumber.Text + ".ttl"; } StoreItems<SoldItem> lstSelectedItems = new StoreItems<SoldItem>(); using (FileStream fsSelectedItems = new FileStream(FilenameSelectedItem, FileMode.Create)) { // Get the items of each row and write its values to the selected items file for (int i = 0; i < dgvSelectedItems.Rows.Count - 1; i++) { SoldItem si = new SoldItem(); si.ItemNumber = dgvSelectedItems.Rows[i].Cells[0].Value.ToString(); si.ItemName = dgvSelectedItems.Rows[i].Cells[1].Value.ToString(); si.UnitPrice = double.Parse(dgvSelectedItems.Rows[i].Cells[2].Value.ToString()); si.Quantity = int.Parse( dgvSelectedItems.Rows[i].Cells[3].Value.ToString()); si.SubTotal = double.Parse(dgvSelectedItems.Rows[i].Cells[4].Value.ToString()); lstSelectedItems.Add(si); } BinaryFormatter bfSelectedItems = new BinaryFormatter(); bfSelectedItems.Serialize(fsSelectedItems, lstSelectedItems); } using (StreamWriter wrtTotals = new StreamWriter(FilenameTotal)) { wrtTotals.WriteLine(txtItemsTotal.Text); wrtTotals.WriteLine(txtTaxRate.Text); wrtTotals.WriteLine(txtTaxAmount.Text); wrtTotals.WriteLine(txtOrderTotal.Text); } // Reset the form btnNewCustomerOrder_Click(sender, e); }
- Return to the Music Store Form
- Double-click the Open button and implement its event as follows: private void btnOpen_Click(object sender, EventArgs e) { var strDirSelectedItems = @"C:\Solo Music Store\Receipts\Selected Items"; var strFileSelectedItems = @"C:\Solo Music Store\Receipts\Selected Items\1000.its"; var strDirTotals = @"C:\Solo Music Store\Receipts\Totals"; var strFileTotals = @"C:\Solo Music Store\Receipts\Totals\1000.ttl"; if (txtReceiptNumber.Text == "") return; else { strFileSelectedItems = strDirSelectedItems + "\\" + txtReceiptNumber.Text + ".its"; if (File.Exists(strFileSelectedItems)) { using (FileStream fsSavedItems = new FileStream(strFileSelectedItems, FileMode.Open)) { BinaryFormatter bfSaveItems = new BinaryFormatter(); StoreItems<SoldItem> lstSavedItems = (StoreItems<SoldItem>)bfSaveItems.Deserialize(fsSavedItems); dgvSelectedItems.Rows.Clear(); for(int i = 0; i < lstSavedItems.Count; i++) { dgvSelectedItems.Rows.Add(); SoldItem si = (SoldItem)lstSavedItems[i]; dgvSelectedItems.Rows[i].Cells[0].Value = si.ItemNumber; dgvSelectedItems.Rows[i].Cells[1].Value = si.ItemName; dgvSelectedItems.Rows[i].Cells[2].Value = si.UnitPrice.ToString(); dgvSelectedItems.Rows[i].Cells[3].Value = si.Quantity; dgvSelectedItems.Rows[i].Cells[4].Value = si.SubTotal; } } strFileTotals = strDirTotals + "\\" + txtReceiptNumber.Text + ".ttl"; try { var rdrTotals = new StreamReader(strFileTotals); try { txtItemsTotal.Text = rdrTotals.ReadLine(); txtTaxRate.Text = rdrTotals.ReadLine(); txtTaxAmount.Text = rdrTotals.ReadLine(); txtOrderTotal.Text = rdrTotals.ReadLine(); IsNewCustomerOrder = false; } finally { rdrTotals.Close(); } } catch (FileNotFoundException) { MessageBox.Show("There is no customer order " + "with that receipt number"); } } } }
- Return to the Music Store form
- Double-click an unoccupied area of the form
- Implement the event as follows: private void MusicStore_Load(object sender, EventArgs e) { btnNewCustomerOrder_Click(sender, e); LoadMusicStore(); IsNewCustomerOrder = true; }
- Return to the Music Store form
- Double-click the Close and implement its Click event as follows: private void btnClose_Click(object sender, EventArgs e) { Close(); }
- Execute the application
- Access the Item Editor dialog box and create a few more items as follows (let the computer create the item numbers): Category Type Item Name Unit Price Picture Recording Microphone MXL V63M Studio Condenser Microphone 72.95 Picture Guitars Acoustic Yamaha FD01S Acoustic Folk Guitar 185.95 Book/CD/DVD Instructional Hal Leonard Amazing Phrasing - Alto Sax (Book/CD) 18.00 Guitars Classical & Nylon Alvarez Artist Series AC60S Classical Acoustic Guitar 275.95 Picture Guitars Acoustic Washburn D100DL Acoustic Guitar 150.50 Picture Drums Electronic Percussion Boss DR-670 Dr. Rhythm Drum Machine 275.85 Picture Recording Microphone Shure SM58 Mic 95.95 Picture Accessories Cables Live Wire HPE325 Headphone and Extension Cable 10.95 Picture Bass Acoustic Fretted Ibanez AEB10E Acoustic-Electric Bass Guitar with Onboard Tuner 350.00 Picture Drums World Percussion Latin Percussion Conga 3-Pack with Bongos 595.95 Picture Keyboards Synthesizers Roland JUNO-D 61-Key Synthesizer 595.95 Picture Drums World Percussion Latin Percussion Aspire Conga Set with Bongos and Stand 425.50 Picture Recording Microphone AKG Perception 200 Condenser Microphone 160.00 Picture
- Create a few customers' orders and click Save then New Order each time. Here are examples:
- Close the form and return to your programming environment
Checking the Existence of an Item#
One of the routine operations you can perform on a list is to find out whether it contains a certain value. To assist you with this operation, the IList<> interface is equipped with a method named Contains. Its syntax is:
bool Contains(T item);This method takes as argument the value to look for. If the value is found in the collection, the method returns true. If no value is found in the collection, this method returns false. Here is an example of implementing this method:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
public bool Contains(T value)
{
for (int i = 0; i < Count; i++)
if (items[i].Equals(value))
return true;
return false;
}
}This method calls the Equals() method of the objects that make up the list to find out whether the value argument exists in the collection. If this method produces a wrong result, especially if you are using your own class to represent the item, you should override the Equals() method.
Getting the Index of an Item#
The Contains() method is used to check whether a particular value (already) exists in the collection. If you know that a certain item exists but you don't know its index inside the list, the IList<> interface can assist you through a method named IndexOf. Its syntax is:
int IndexOf(T item);This method takes as argument the value to look for in the list. If the value is found in the collection, the method returns its index. If there is no value defined like that, the method returns -1. Here is an example of implementing this method:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
public int IndexOf(T value)
{
for (int i = 0; i < Count; i++)
if (items[i].Equals(value))
return i;
return -1;
}
}Once again, this method calls the Equals() method of the objects that make up the collection. In most cases, you should make sure you override the Equals() method in the class that T represents.
Deleting Items#
Deleting a Value by its Index#
If a value is not necessary in your collection, you can delete it. Probably the simplest way to delete a value is to specify its position in the list. To support this operation, both the System.Collections.IList and the System.Collections.Generic.IList interfaces are equipped with a method named RemoveAt. The syntax of the RemoveAt() method is is the same for both interfaces and it is:
void RemoveAt(int index);This method takes as argument the index of the value to be removed. If the index is valid, the method removes it. If the index is not valid, the compiler will throw and ArgumentOutOfRangeException. Here is an example:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// This method is used to delete an item based on its position
public void RemoveAt(int index)
{
// First check that the user provided a positive index that
// is lower than the current total number of items
if ((index >= 0) && (index < size))
{
// If so, starting at that index, get the item ahead of
// each position and assign it to the current item
for (int i = index; i < size - 1; i++)
items[i] = items[i + 1];
// Since the last item is not useful anymore,
// decrease the number of items
size--;
}
}
}If the item cannot be deleted for another reason, the compiler would throw an NotSupportedException.
Deleting an Item by its Value#
The problem with deleting a value based on its index is that, if you are not sure, you could delete the wrong value. An alternative is to first precisely define the value you want to get rid of, and then hand the value itself to the compiler that would remove it. To support this approach, the System.Collections.IList interface is equipped with a method named Remove() and whose syntax is:
bool Remove(T item);This method takes as argument the value to be deleted. This means that the compiler will first look for the value in the list. If it finds that value, it removes it. If there is no value like that, nothing happens. Here is an example of implementing this method:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// This method is used to delete an item
public bool Remove(T value)
{
// First try to get the index of the item to find out if it exists
int index = IndexOf(value);
// If the item was found ...
if (index >= 0)
{
// ... delete it, using its index
RemoveAt(index);
// Since the item has been removed, return true
return true;
}
// If the item was not removed (for any reason), return false
return false;
}
}Clearing a Collection#
To remove all values from a list at once, you can implement Clear() method of the IList<> interface. Its syntax is:
void Clear();Here is an example of implementing it:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
public Collection()
{
size = 0;
items = new T[10];
}
public void CopyTo(T[] array, int arrayIndex)
{
T[] values = new T[size];
for (int i = 0; i < size; i++)
values[i] = items[i];
array = values;
}
public int Count
{
get { return size; }
}
public bool IsReadOnly
{
get { return false; }
}
private void CheckAndIncreaseIfNecessary()
{
if (size >= items.Length)
Array.Resize<T>(ref items, items.Length + 5);
}
// The index property can be used to add an item to a collection,
// or to retrieve an item from the collection
public T this[int index]
{
get
{
return this.items[index];
}
set
{
this.items[index] = value;
}
}
public void Add(T value)
{
// Check whether there is still room in the array.
// If there is not enough room, then increase the capacity
CheckAndIncreaseIfNecessary();
// Add the item at the end
this.items[this.size] = value;
// Increase the current number of items
this.size++;
}
public void Insert(int index, T value)
{
// If the index is the same as the current number of items
// then call Add() and proceed as if we want to add a new item
if (index >= size)
{
Add(value);
return;
}
// If the index is positive but less than the current size,
// then you need to insert the item inside the collection.
if ((index >= 0) && (index < size))
{
// Firs, push each item one position up to create
// an empty space
for (int i = (size - 1); i > index - 1; i--)
items[i + 1] = items[i];
// Then put the new item in the indicated position
items[index] = value;
// Since the new item has been added, increase the
// current number of items
size++;
}
}
public bool Contains(T value)
{
for (int i = 0; i < Count; i++)
if (items[i].Equals(value))
return true;
return false;
}
public int IndexOf(T value)
{
for (int i = 0; i < Count; i++)
if (items[i].Equals(value))
return i;
return -1;
}
// This method is used to delete an item based on its position
public void RemoveAt(int index)
{
// First check that the user provided a positive index that
// is lower than the current total number of items
if ((index >= 0) && (index < size))
{
// If so, starting at that index, get the item ahead of
// each position and assign it to the current item
for (int i = index; i < size - 1; i++)
items[i] = items[i + 1];
// Since the last item is not useful anymore,
// decrease the number of items
size--;
}
}
// This method is used to delete an item
public bool Remove(T value)
{
// First try to get the index of the item to find out if it exists
int index = IndexOf(value);
// If the item was found ...
if (index >= 0)
{
// ... delete it, using its index
RemoveAt(index);
// Since the item has been removed, return true
return true;
}
// If the item was not removed (for any reason), return false
return false;
}
// This method is used to delete all items from a collection
public void Clear()
{
// Visit each item in the collection and set it to the default value
// (This is not really necessary)
for (int i = 0; i < size; i++)
items[i] = default(T);
// Reset the number of items of the collection to 0
size = 0;
}
public IEnumerator<T> GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
}Additional Operations on a Collection#
Introduction#
The operations we saw above are the most regular of a collection. Still, there are many other ways you can assist a user of your collection class by defining more functionality. For example, you can make it possible to add more than one item with a single action, to delete more than one item in one step, to move items inside the collection by changing their positions, etc. The options are infinite as long as you have imagination and want to make your class more useful.
Checking Whether the Collection is Empty#
Checking the emptiness of a collection allows you to find out whether it contains at least one item. Probably the easiest way to get this information is to enquire about the total number of items ini the list. This can be done as follows:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// Checks whether the list is empty
public bool IsEmpty()
{
if (size == 0)
return true;
return false;
}
}Adding a Collection to a Collection#
Instead of adding one item at a time, you can allow a collection to receive a series of items. To do this, you can create a method to which you would pass an argument as argument. In the body of the method, you can use a for loop to navigate from one item to the next. When you get to an item, replace it with an item of the corresponding position of the argument. Here is an example:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
public void AddRange(T[] values)
{
for (int i = 0; i < values.Length; i++)
Add(values[i]);
}
}The First Item of a Collection#
A collection that has at least one item has a starting point. There are various ways you can get the first item of a collection. If you are using an array-based list, you can simply get the item at index 0. Here is an example:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// Gets the first item in the list
public T First
{
get
{
if (IsEmpty())
return default(T);
return items[0];
}
}
}Fortunately, remember that the IList<> is derived from the IEnumerable<> interface. That interface is equipped with a method named First. Its syntax is:
public static TSource First<TSource>(this IEnumerable<TSource> source);This method allows you to get the first member of a collection.
The Last Item of a Collection#
If you are using an array-based list, to get the last item, get the item at the total number of items. Here is an example:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// Gets the last item in the list
public T Last
{
get
{
if (IsEmpty())
return default(T);
return items[size - 1];
}
}
}Remember that the IList<> derives from the IEnumerable<> interface. That interface provides a method named Last. Its syntax is:
public static TSource Last<TSource>(this IEnumerable<TSource> source);This method allows you to get the first member of a collection.
Replacing an Item#
We saw that, using the indexed property, you can replace an existing item. This operation is not too difficult. You can proceed exactly as done with the set accessor of the indexed property, except that you should check the validity of the index. Here is an example:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// Replaces an existing item in the list with a new item
// Equivalent to editing an existing item
public void Replace(int index, T one)
{
if (index < 0)
{
// Invalid position
// Normally, we should throw an exception, such as IndexOutOfRangeException
return;
}
if (index > size)
{
// Invalid position
// Normally, we should throw an exception, such as IndexOutOfRangeException
return;
}
items[index] = one;
}
}Moving an Item#
Moving an item consists of changing its position. That is, the item's index changes from where it is, the source, to another position, the target. To perform this operation, you can pass two positions to a method. When defining the method, first make sure the positions are valid. Whenever any position is not valid, you should cancel the operation and throw an exception. If the positions are valid:
- Save the item of the source position to a temporary object
- Delete the item at the target index
- Insert the saved item in the target index
Here is an example:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// Changes the current position of an existing item.
// Moves the item from its current curIndex position to the new toIndex position
public void Move(int curIndex, int toIndex)
{
if (curIndex < 0)
{
// Invalid starting index
// Normally, we should throw an exception, such as IndexOutOfRange
return;
}
if (curIndex > (size - 1))
{
// Invalid starting index
// Normally, we should throw an exception, such as IndexOutOfRange
return;
}
if (toIndex < 0)
{
// Invalid end index
// Normally, we should throw an exception, such as IndexOutOfRange
return;
}
if (toIndex > (size - 1))
{
// Invalid end index
// Normally, we should throw an exception, such as IndexOutOfRange
return;
}
if (curIndex == toIndex)
{
// Same index, do nothing
return;
}
// Get the item at the starting position
T objLower = this[curIndex];
// Remove the item at the starting position
RemoveAt(curIndex);
// move the item to target position
Insert(toIndex, objLower);
}
}Swapping Items#
Swapping two items consists of exchanging their positions. That is, the item at index x will move to index y and the item at index y will move to index x. To perform this operation, you pass two positions to a method. In the method, first make sure the positions are valid. If any position is invalid, you should cancel the operation and throw an exception. If the positions are valid:
- Temporarily save one of the items
- Temporarily save the other item
- Put the first saved item in the other position by replacing it
- Put the other saved item in the first position by replacing it
Here is an example of how this can be done:
public class Collection<T> : IList<T>
{
private int size;
private T[] items;
. . .
// Swaps the positions of two existing items
// The first item goes to position posSecond
// and the second item moves to position posFirst
public void Exchange(int posFirst, int posSecond)
{
try
{
// Check that posFirst exists
if (posFirst < 0)
throw new IndexOutOfRangeException("Invalid first index - Too low");
// Check that posFirst exists
if (posFirst > size - 1)
throw new IndexOutOfRangeException("Invalid first index - Too high");
// Check that posSecond exists
if (posSecond < 0)
throw new IndexOutOfRangeException("Invalid second index - Too low");
// Check that posSecond exists
if (posSecond > size - 1)
throw new IndexOutOfRangeException("Invalid second index - Too high");
T objFirst = this[posFirst];
T objSecond = this[posSecond];
this[posFirst] = objSecond;
this[posSecond] = objFirst;
}
catch (IndexOutOfRangeException)
{
}
}
}Home Copyright © 2010 C# Key

