Working with Loops, Arrays, and Collections in VBNET Game Development - Collections
(Page 3 of 4 )
Arrays are fine for some circumstances, but in other circumstances, such as when the length of a group is a variable or when more complex behavior (e.g. a key, value combination) is needed, arrays aren't enough for the job. That's where collections come into play. These are collections of objects more complex than arrays.
There are two groups of collections in .NET. The first group of collections contains non-generic collections. They were implemented in Visual Basic first, but they are not type-safe, and so you can stick any type of object in one of these collections no matter what type the other elements are or how the collection is used. This isn't a good thing and we won't be using these collections. The second group of collections contain generic collections. When a collection is created, you must specify a type for the elements. This type is enforced and so you can't mix and match types. We'll be looking at generic collections.
The most array-like collection is List(T) (the “T” in “(T)” stands for “type” and it indicates that we're using a generic class). It's created like this:
Dim collection1 As New List( Of Integer )
Notice how we specify the type name with the Of keyword. This is done with all generics and, as noted above, this type is enforced. We can only add Integer s to the collection, just as we can only add Integer s to an Integer array. Also notice how we don't specify an upper bound as we do with arrays. This is because collections are not confined to a fixed size. Rather, you can add and remove elements as needed. We can add elements to the collection using the Add procedure:
collection1.Add(1)
collection1.Add(2)
collection1.Add(3)
If we need to remove an element, we can use the Remove method, which accepts an object and then removes the first occurrence of that object. Let's remove the number 3 from our collection:
collection1.Remove(3)
We can also use RemoveAt, which removes the element at a given index. Let's remove the first element:
collection1.RemoveAt(0)
Or, if we want to remove every element, we can use Clear:
collection1.Clear()
The size of the collection can be obtained using the Count property:
Console.WriteLine("Length: {0}", collection1.Count)
Another collection is the Dictionary (TKey, TValue) collection. It's similar to what other languages may call an associative array or a hash table. Instead of matching elements to consecutive zero-based indexes, a dictionary maps values to keys. For example, say we want to associate names and ages. We could use a dictionary for this where the name is the key and the age is the value. The name, of course, would be stored as a String, and the age would be stored as an Integer. Let's create a Dictionary that matches this situation:
Dim people As New Dictionary( Of String , Integer )
Key-value pairs are added using Add :
people.Add("Bob", 35)
people.Add("Henry", 40)
We can remove elements using Remove, which removes an element with a given key:
people.Remove("Henry")
There are more collections out there, and even the ones that have been mentioned have more to them, but we'll worry about that later on if we need to. A full tutorial on even the basic collections and their members would take up quite a bit of space.
Next: For Each...Next (For Real This Time) >>
More Visual Basic.NET Articles
More By Peyton McCullough