| Author: mythili d 05 Sep 2008 | Member Level: Silver | Rating: Points: 3 |
collections are like the arrays. The only difference being they can hold not all same type of data for example an array list can hold int,char,string float etc all at the same time. In arrays if you wanted to create asuch a structure you will hhave to create different arrays with all different types. a1 hold int a2 to hold char a3 for strings a4 float etc. But in this kind of code there could be runtime problems like you could have imagined at runtime because of the type mismatch. so in the later versions of .net you can use array list and other types of generic collections to hold objects of all same type.
There are two types of collection like you mentioned the generic and the non generic type.
Generic collection: To explain this I would like to explain the array first. In arrays iif the array is declared to be of int then you can use the same array variable to store the array size number of integers. all refernce by the same name. Similarly in the array list we can use to store the user created objects. for example employee. eg Daemployee emp= new Daemployee(); List<emp> mylistemp=List<emp>;
//for retrieving you could use for each ( emp in mylistemp) { //to process code here }
but however there could be problems when you have two diff type of objects and they have everything common like employee (id,firstname.lastname) Person(id,firstname,lastname) You could use this list to store either a employee or the person. And there is still a problem with data because it is not all of asame type. This is a human error in coding and has got nothing to do with the way these are define. Besides using a array list there is no other easy way to store an array of object to process them.
The second type is the Dictionary whihc could store the name and value in the same variable name . This works exactly like the hastables. With has an advantage over because they have type safety etc. because dictionary is a generic collection. where as hashtables are not.
DataRow jdeItemRow = DAItem.GetItemForJDEByID(itemID); System.Collections.Generic.Dictionary<string, object> vals = new Dictionary<string, object>();
vals.Add("StyleNum", jdeItemRow["StyleNum"].ToString()); vals.Add("Division", jdeItemRow["Division"].ToString()); vals.Add("Category", jdeItemRow["Category"].ToString()); InsertRow(vals, "tblJDELiveExport");
So that is the example from my code. This the simplest way we could have used the dictionary collection.
I have not used the Hashtables a lot so cannot talk about them. Hope this made it easier for you to study more about these collections. thanks
|