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.
- HashTable hst = new HashTable();
- hst.Add(<Key>,<Value>);
- 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);
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.
- 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 query on 'Ricardo Jonperez' 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();
- }
- }
- }
Join the conversation! Your thoughts help the community grow.