Home

Collection Classes and Databases

 

Introduction

A database is collection of values. To create these values, you can use a collection class. Fortunately, instead of creating a class from scratch, the .NET Framework provides a very impressive library of interfaces and collection classes. The built-in interfaces of the .NET Framework lay a foundation that other classes use to implement and customize the desired functionality. The various built-in collection classes are meant to satisfy various purposes. Some classes are available to any application and can be used by any Windows control. Some other collection classes are nested in classes that particularly need them.

The .NET Framework supports collections in various namespaces. While the System.Collections namespace provides regular collection classes, the System.Collections.Generic namespace contains the equivalent generic classes. 

Accessories for Collections

One the most routines operations performed on a database consists of reviewing its values. To assist you with this, the .NET Framework provides the IEnumerator and the IEnumerable interfaces that are defined in the System.Collections namespace. Their generic equivalences can be found in the System.Collections.Generic namespace. After implementing these interfaces, you can use the foreach operator to visit each value of the database.

To implement the System.Collections.IEnumerator interface, you must derive a class from it. Then, you must define the Reset(), the MoveNext() methods, and the Current property. Here is an example:

using System;
using System.Collections;

namespace Exercise
{
    public class Enumerator : IEnumerator 
    {
        private string[] names;
        private int cur;

        public Enumerator(string[] list)
        {
            this.names = list;
            cur = -1;
        }

        public Object Current
        {
            get { return names[cur]; }
        }

        public void Reset()
        {
            cur = -1;
        }

        public bool MoveNext()
        {
            cur++;

            if (cur < names.Length)
                return true;
            else
                return false;
        }
    }
}

To implement the System.Collections.IEnumerable interface, you must derive a class from it. When implementing the class, you must define an accessory method and the GetEnumerator() method that returns an IEnumerator object. Here is an example: 

using System;
using System.Collections;

namespace Exercise
{
    class Enumerable : IEnumerable
    {
        private string[] names;

        public void Identify(string[] values)
        {
            names = values;
            for (int i = 0; i < values.Length; i++)
                names[i] = values[i];
        }

        public IEnumerator GetEnumerator()
        {
            return new Enumerator(names);
        }
    }
}

Once you have implemented the interfaces, you can use foreach. Here is an example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Exercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] FullNames = new string[8];

            FullNames[0] = "Gertrude Monay";
            FullNames[1] = "Paul Bertrand Yamaguchi";
            FullNames[2] = "Hermine Ngaleu";
            FullNames[3] = "Francine Mukoko";
            FullNames[4] = "Joseph Walters";
            FullNames[5] = "Patricia Katts";
            FullNames[6] = "Helen Cranston";
            FullNames[7] = "Paul Motto";

            Enumerable coll = new Enumerable();

            coll.Identify(FullNames);
            foreach (string s in coll)
                lbxNames.Items.Add(s);
        }
    }
}

IEnumerator, IEnumerable, and foreach

Choosing an Interface

While the IEnumerator and the IEnumerable interfaces serve as valuable accessories that allow a collection class to support enumeration, to actually create a collection class, there are other interfaces you can use to implement the functionality you want for your collection.

When you want to use a collection in your application, you may first check what classes are available in the .NET Framework. If you don't find a suitable class, you can create your own that implements one or more interfaces. As it happens, the .NET Framework ships with many of them and your next step is to choose which one you prefer. Some of the most commonly used interfaces are

  • System.Collections.IComparer and System.Collections.Generic.IComparer:If you derive a class from this interface, you can define how two objects would be compared for similarity or difference
  • System.Collections.IDictionary and System.Collections.Generic.IDictionary: This interface is used to create a collection class where each item is made of a  key=value combination
 

Home Copyright © 2007 FunctionX, Inc. Next