FlexibleList in C#

Today, I have the opportunity to bring you some useful C# gifts that hopefully are useful in our daily development activities. These gifts might help in developing database apps or other types of software as well. These gifts are the classes OBList<T> and OBItem<T>. I want to introduce these classes to this community.

Now, let's pay some attention and see what these classes are and how to use these classes in our code.

The main point here is the class OBList<T>. Before I dig further to the forestry of codes, I want to tell you that I was inspired to write a class dll that can help me in managing database elements such as a Column Field, the Operator Clause like " LIKE ", " = " , " < ", " > ", " <= ", " >=", ..etc. For example : " LastName LIKE '%Wu%' ". The Class OBList<T> itself is inherited from the System.Collections.Generic.List<OBItem<T>>. Class OBItem<T> itself is a class that acts like a Dictionary which has a Key -> Value Pair contained in it. So OBList<T> encapsulates the OBItem<T>, in the usage of an instance of OBList<T>, we just only need to add the core data item which is "the value part", the key part is initialized with the automatically increasing of ItemIndex on each called to the AddItem method of OBList<T>.
Or you can assign other type values to the key part later on. Compare to our well-known .NET key-value pair Dictionary or HashTable Class in which we must explicitly specify the key-value part while adding a new item in it. Such as:

...................................................
HashTable hst = new HashTable();
hst.Add(<Key>,<Value>);
..................................................

Instead of that, consider my OBList<T> special characteristic such as:

...................................................................................................................................

OBList<string> obst = new OBList<string>();

obst.AddItem("some value data");

Console.WriteLine("Key value is : {0}, Data value is : {1}",obst[0].OBKey,obst[0].OBContent);
....................................................................................................................................

Although it is a good example of holding an array of sets, suppose we want to add a number of items from a ComboBox control to our list, then this is easier than the usual .NET list.

I compiled it successfully in VS2008:
The complete code is listed below:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
 

namespace
ConsoleApplication6
{
    class
Program
    {
        class OBItem<T>
        {
            private int _nitem;
            private Object _mkey; T _mcontent;
 
            public OBItem() { _nitem++; }
 
            public OBItem(Object keyId, T obContent)
            {
                _mkey = keyId; _mcontent = obContent; _nitem++;
            }
 
            ~OBItem() { }
 
            public Object OBKey
            {
                get { return _mkey; }
                set { _mkey = value; }
            }
 
            public T OBContent
            {
                get { return _mcontent; }
                set { _mcontent = value; }
            }
 
            public int ItemIndex
            {
                get { return _nitem; }
                set { _nitem = value; }
            }
 
        }
 
        class OBList<T> : List<OBItem<T>>
        {
            private static int nctr = 0;
            public OBList() : base() { }
            ~OBList() { }
 
            public void AddItem(T objItem)
            {
                OBItem<T> oTemp = new OBItem<T>(); nctr++;
                oTemp.ItemIndex = nctr;
                oTemp.OBKey = nctr;
                oTemp.OBContent = objItem;
                base.Add(oTemp);
                oTemp = null;
            }
 
            public void OBSort<U>(string st)
            {
                base.Sort(delegate(OBItem<T> ob1, OBItem<T> ob2)
                {
                    return ob1.OBContent.ToString().CompareTo(ob2.OBContent.ToString());
                });
            }
 
            public void OBSort<V>(int nt)
            {
                base.Sort(delegate(OBItem<T> obn1, OBItem<T> obn2)
                {
                    return Int32.Parse(obn1.OBContent.ToString()).CompareTo(Int32.Parse(obn2.OBContent.ToString()));
                });
            }
 
            public new OBItem<T> this[int nItem]
            {
                get { if (nItem <= base.Count - 1) return base[nItem]; else return null; }
                set { if (nItem <= base.Count - 1) base[nItem] = value; }
            }
 
 
            public int this[T searchTarget]
            {
               
get
                {
                    int nfound = -1; base.Find(delegate(OBItem<T> srchItm)
                    {
                        if (srchItm.OBContent.Equals(searchTarget)) { nfound = srchItm.ItemIndex; return true; }

                        else return false;
                    });
                    return nfound;
                }
            }
        }
 
        static void Main(string[] args)
        {
            OBList<string> mobList = new OBList<string>();
            OBList<int> numList = new OBList<int>();
 
            Console.WriteLine(" UnSorted OBList<string> ");
            Console.WriteLine("-------------------------------------------------------");
 
            mobList.AddItem("mailto:Eddy%20Wu%20-%[email protected]");
            mobList.AddItem("mailto:Sherly%C2%A0%20-%[email protected]");
            mobList.AddItem("mailto:Niki%20Oktavia%20-%[email protected]");
            mobList.AddItem("mailto:Ronny%20Chandra%20-%[email protected]");
            mobList.AddItem("mailto:Tony%20Rolandio%20-%[email protected]");
            mobList.AddItem("mailto:Yohannes%20-%[email protected]");
            mobList.AddItem("mailto:Caren%20Johnsosn%20-%[email protected]");
            mobList.AddItem("mailto:David%20Jordan%20-%[email protected]");
            mobList.AddItem("mailto:Ricardo%20Jonperez%20-%[email protected]");
 
            for (int nr = 0; nr < mobList.Count() - 1; nr++)
                Console.WriteLine(mobList[nr].OBContent); Console.WriteLine();
 
            Console.WriteLine(" UnSorted OBList<int> ");
            Console.WriteLine("----------------------------------------------------");

            numList.AddItem(9000);
            numList.AddItem(5000);
            numList.AddItem(7000);
            numList.AddItem(2500);
            numList.AddItem(10500);
            numList.AddItem(5750);

            for (int nt = 0; nt < numList.Count() - 1; nt++)
                Console.WriteLine(numList[nt].OBContent); Console.WriteLine();
 
            mobList.OBSort<string>("abc");
 
            Console.WriteLine(" After Sorted OBList<string> :");
            Console.WriteLine("-------------------------------------------------------------");
             for (int nc = 0; nc < mobList.Count() - 1; nc++)
                Console.WriteLine(mobList[nc].OBContent); Console.WriteLine();
 
            numList.OBSort<int>(100); Console.WriteLine(" After Sorted OBList<int> : ");
            Console.WriteLine("-------------------------------------------------------------");
            for (int nj = 0; nj < numList.Count() - 1; nj++)
                Console.WriteLine(numList[nj].OBContent); Console.WriteLine();
  
            Console.WriteLine(" Now perform a search on value 'Ricardo Jonperez' contained in OBList<string> ");
 
            if (mobList["Ricardo Jonperez - [email protected]"] >= 0)
                Console.WriteLine(" 'Ricardo Jonperez' Found at Element {0}..", mobList["Ricardo Jonperez - [email protected]"]);
            else Console.WriteLine("Search not found..");
 
            Console.WriteLine();
 
            Console.WriteLine("Now set a new OBItem<T> into element 5 of OBList<T> :");
            mobList[5] = new OBItem<string>(5, "Giovanni Belucci"); Console.WriteLine();
 
            Console.WriteLine(" The Fifth Element of OBList<string> is: {0}", mobList[5].OBContent);
            Console.WriteLine(); mobList.OBSort<string>("xyz"); Console.WriteLine();
 
             Console.WriteLine(" Re-Sorted OBList<string> ... ");
            Console.WriteLine("----------------------------------------------------------------");
 
            for (int nm = 0; nm < mobList.Count() - 1; nm++)
                Console.WriteLine(mobList[nm].OBContent);
 
            Console.WriteLine();
 
            Console.WriteLine(" Press any key to succeed.. ");
            Console.ReadKey();
        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all