FlexibleList In C#

This article is about FlexibleList in C#.

Today I want to introduce you some classes design that I think it might tend to be worthsome to be used in any database oriented application development. The classes design involves a list-like structure and a data structure element class. They are OBList<T> for the list class design and OBItem<T> for the data structure class design.

Most of us may had familiarized with any database query keywords that used to query a specific record which met the criteria of a specified search constraint. The most favorably query keyword maybe the " LIKE " statement that had invariably used in almost any database operation. For example: " LastName LIKE ' %Wu% ' ". The design made to these classes are so simple that to the any extend it even allow a LinQ statement query over its instance.
 
The class OBList<T> is derived from the System.Collections.Generic.List< OBItem<T> >. The OBItem<T> which is a data element to the list adopts a key->value pair like structure to its class design. The class OBList<T> will encapsulate the OBItem<T> data element class. It is to say that an OBItem<T> object is a data element to the OBList<T> instance. From those designs workout, because OBList<T> is derived from the standard System.Collections.Generic.List<T> class, we can then cozy up with the using of any standard functionality derived in the OBList<T> for any sort of managements that may incurred to the number of OBItem<T> elements in the list. The System.Collections.Generic.List of the .NET Standard class had a bunches of mostly useful methods and properties for any sort of data managements that may incurred to its elements in the list. Such a standard method even imply an anonymous delegate to its sort procedure so we can get more easy and felt lightness in the coding job itself. Other fascinating facility of the standard class involves a this member property of the class which can enumerate to any data element in the list as long as the key index used is any derivation of the .NET Object class. Any extending capability of the .NET standard List<T> class includes an ability to receipt a LinQ query statement used to query data over the structure of its instance.
 
The use of OBList<T> in the program design is quite simple and not complicated. You should first defined any data element of OBItem<T> that you want to add to the list of OBList<T>. We then use looping to iterate through number of OBItem<T> elements in the list for the add, delete and modify of OBItem<T> object in the list. The standard List<T> class provides the " Add " method for adding any new item object to its internal storage. The "Add" operation is so simple that you only need to provide the data " value " part as argument to the method, the key part is automatically calculated for you. Soon you'll see how can this be performed and be possible throughout the coding.
 
In compare to the standard .NET key->value pair Dictionary or HashTable class to either of which we should specify completely the key and value part of the data element be added like the following illustration describes about. 
  1. HashTable hst = new HashTable();  
  2. hst.Add(<Key>,<Value>);  
Instead of that, consider my OBList<T> special characteristic such as,
  1. OBList<string> obst = new OBList<string>();  
  2. obst.AddItem("some value data");  
  3. Console.WriteLine("Key value is : {0}, Data value is : {1}",obst[0].OBKey,obst[0].OBContent);  

Beside it is a good example of holding an array of sets, it even can turn itself to support multifaceted of data needed in the program. Supposes we want to add number of items from a ComboBox to the list, then this can become more easier than the usual standard defined.

Here are complete listing of the classes design and implementations shown below. If you found this can or maybe useful you can deploy it yourself to your own C# project. Thanks.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.    
  6. namespace ConsoleApplication6  
  7. {  
  8.     class Program  
  9.     {   
  10.         class OBItem<T>  
  11.         {  
  12.             private int _nitem;  
  13.             private Object _mkey; T _mcontent;  
  14.    
  15.             public OBItem() { _nitem++; }  
  16.    
  17.             public OBItem(Object keyId, T obContent)  
  18.             {  
  19.                 _mkey = keyId; _mcontent = obContent; _nitem++;  
  20.             }  
  21.    
  22.             ~OBItem() { }  
  23.    
  24.             public Object OBKey  
  25.             {  
  26.                 get { return _mkey; }  
  27.                 set { _mkey = value; }  
  28.             }  
  29.    
  30.             public T OBContent  
  31.             {  
  32.                 get { return _mcontent; }  
  33.                 set { _mcontent = value; }  
  34.             }  
  35.    
  36.             public int ItemIndex  
  37.             {  
  38.                 get { return _nitem; }  
  39.                 set { _nitem = value; }  
  40.             }  
  41.    
  42.         }   
  43.    
  44.         class OBList<T> : List<OBItem<T>>  
  45.         {  
  46.             private static int nctr = 0;  
  47.             public OBList() : base() { }  
  48.             ~OBList() { }   
  49.    
  50.             public void AddItem(T objItem)  
  51.             {  
  52.                 OBItem<T> oTemp = new OBItem<T>(); nctr++;  
  53.                 oTemp.ItemIndex = nctr;  
  54.                 oTemp.OBKey = nctr;  
  55.                 oTemp.OBContent = objItem;  
  56.                 base.Add(oTemp);  
  57.                 oTemp = null;  
  58.             }  
  59.    
  60.             public void OBSort<U>(string st)  
  61.             {  
  62.                 base.Sort(delegate(OBItem<T> ob1, OBItem<T> ob2)  
  63.                 {  
  64.                     return ob1.OBContent.ToString().CompareTo(ob2.OBContent.ToString());  
  65.                 });  
  66.             }   
  67.    
  68.             public void OBSort<V>(int nt)  
  69.             {  
  70.                 base.Sort(delegate(OBItem<T> obn1, OBItem<T> obn2)  
  71.                 {  
  72.                     return Int32.Parse(obn1.OBContent.ToString()).CompareTo(Int32.Parse(obn2.OBContent.ToString()));  
  73.                 });  
  74.             }   
  75.    
  76.             public new OBItem<T> this[int nItem]  
  77.             {  
  78.                 get { if (nItem <= base.Count - 1) return base[nItem]; else return null; }  
  79.                 set { if (nItem <= base.Count - 1) base[nItem] = value; }  
  80.             }  
  81.    
  82.    
  83.             public int this[T searchTarget]  
  84.             {  
  85.                 get  
  86.                 {  
  87.                     int nfound = -1; base.Find(delegate(OBItem<T> srchItm)  
  88.                     {  
  89.                         if (srchItm.OBContent.Equals(searchTarget)) { nfound = srchItm.ItemIndex; return true; }  
  90.   
  91.                         else return false;  
  92.                     });  
  93.                     return nfound;  
  94.                 }  
  95.             }  
  96.         }   
  97.    
  98.         static void Main(string[] args)  
  99.         {  
  100.             OBList<string> mobList = new OBList<string>();  
  101.             OBList<int> numList = new OBList<int>();  
  102.    
  103.             Console.WriteLine(" UnSorted OBList<string> ");  
  104.             Console.WriteLine("-------------------------------------------------------");  
  105.    
  106.             mobList.AddItem("mailto:Eddy%20Wu%20-%20eddywu32@gmail.com");  
  107.             mobList.AddItem("mailto:Sherly%C2%A0%20-%20xxxmail@yahoo.com");  
  108.             mobList.AddItem("mailto:Niki%20Oktavia%20-%20xxxmail@yahoo.com");  
  109.             mobList.AddItem("mailto:Ronny%20Chandra%20-%20xxxmail@gmail.com");  
  110.             mobList.AddItem("mailto:Tony%20Rolandio%20-%20xxxmail@hotmail.com");  
  111.             mobList.AddItem("mailto:Yohannes%20-%20xxxmail@gmail.com");  
  112.             mobList.AddItem("mailto:Caren%20Johnsosn%20-%20xxxmail@hotmail.com");  
  113.             mobList.AddItem("mailto:David%20Jordan%20-%20xxxmail@gmail.com");  
  114.             mobList.AddItem("mailto:Ricardo%20Jonperez%20-%20xxxmail@gmail.com");   
  115.    
  116.             for (int nr = 0; nr < mobList.Count() - 1; nr++)  
  117.                 Console.WriteLine(mobList[nr].OBContent); Console.WriteLine();   
  118.    
  119.             Console.WriteLine(" UnSorted OBList<int> ");  
  120.             Console.WriteLine("----------------------------------------------------");  
  121.   
  122.             numList.AddItem(9000);  
  123.             numList.AddItem(5000);  
  124.             numList.AddItem(7000);  
  125.             numList.AddItem(2500);  
  126.             numList.AddItem(10500);  
  127.             numList.AddItem(5750);  
  128.   
  129.             for (int nt = 0; nt < numList.Count() - 1; nt++)  
  130.                 Console.WriteLine(numList[nt].OBContent); Console.WriteLine();   
  131.    
  132.             mobList.OBSort<string>("abc");  
  133.    
  134.             Console.WriteLine(" After Sorted OBList<string> :");  
  135.             Console.WriteLine("-------------------------------------------------------------");  
  136.              for (int nc = 0; nc < mobList.Count() - 1; nc++)  
  137.                 Console.WriteLine(mobList[nc].OBContent); Console.WriteLine();  
  138.    
  139.             numList.OBSort<int>(100); Console.WriteLine(" After Sorted OBList<int> : ");  
  140.             Console.WriteLine("-------------------------------------------------------------");  
  141.             for (int nj = 0; nj < numList.Count() - 1; nj++)  
  142.                 Console.WriteLine(numList[nj].OBContent); Console.WriteLine();  
  143.     
  144.             Console.WriteLine(" Now perform a search query on 'Ricardo Jonperez' in OBList<string> ");  
  145.    
  146.             if (mobList["Ricardo Jonperez - xxxmail@gmail.com"] >= 0)  
  147.                 Console.WriteLine(" 'Ricardo Jonperez' Found at Element {0}..", mobList["Ricardo Jonperez - xxxmail@gmail.com"]);  
  148.             else Console.WriteLine("Search not found..");  
  149.    
  150.             Console.WriteLine();  
  151.    
  152.             Console.WriteLine("Now set a new OBItem<T> into element 5 of OBList<T> :");  
  153.             mobList[5] = new OBItem<string>(5, "Giovanni Belucci"); Console.WriteLine();  
  154.    
  155.             Console.WriteLine(" The Fifth Element of OBList<string> is: {0}", mobList[5].OBContent);  
  156.             Console.WriteLine(); mobList.OBSort<string>("xyz"); Console.WriteLine();  
  157.    
  158.              Console.WriteLine(" Re-Sorted OBList<string> ... ");  
  159.             Console.WriteLine("----------------------------------------------------------------");  
  160.    
  161.             for (int nm = 0; nm < mobList.Count() - 1; nm++)  
  162.                 Console.WriteLine(mobList[nm].OBContent);  
  163.    
  164.             Console.WriteLine();  
  165.    
  166.             Console.WriteLine(" Press any key to succeed.. ");  
  167.             Console.ReadKey();   
  168.         }  
  169.     }  
  170. }