VB6 and VB.NET collections were easy to use and allowed you to reference an object in the collection either by index or by key. The downside of a VB.NET collection is that it was not strongly typed; therefore, you could put anything into a collection, even objects of differentd types, which seems fairly tricky at best.
There is no direct correlation to a VB.NET or VB6 collection in C#. However, you have several options. First, you can create a strongly typed collection, which is probably the best solution, unless you need to reference objects within the collection by Key. Another option is that you can use a HashTable as a collection and reference the objets by Key. The downside to this is that you cannot reference an object in a HashTable by index; you must use a key. So you are faced with a dilemma when you are moving from VB to C# and have been using collections in VB.
You have to decide whether you need to access the objects by index or by key, and it appears that there is not an easy way to do both.
By the way, if you are not new to C#, you may not want to take time to read this article. This is just something I discovered along the journey while converting a large project from VB to C#.
If you want to get at the objects by key, you use a hash table as follows. First, create the collection
| private Hashtable myCollection = new Hashtable(); |
| int tempFor1 = dt.Rows.Count; foreach (DataRow dr in dt.Rows) { job = new JobObject(); job.Department = dr["department"]; job.WorkType = dr["work_type"]; job.JobNumber = dr["job_number"]; myCollection.Add(job.JobNumber,job); } |
| JobStruct js = (JobStrut) colJobs[123456]; |