Working with Loops, Arrays, and Collections in VBNET Game Development
This is the fifth article of a nine part series that teaches VB.NET via the development of a text-based game. The last article started the discussion of loops, specifically the While loop and the Do loop. This article will complete the discussion of loops and also go over arrays and collections. Please join us as we continue our journey.
Loops are commonly used to loop a set number of times. You're probably familiar with the for loop in other languages. The for loop iterates over a set of numbers. This can allow you to perform some sort of operation on a specific range of numbers, or it can allow you to loop over a set of instructions a set number of times. Either way, it's extremely useful, and Visual Basic, of course, has its own implementation of it in the For ...Next structure.
Visual Basic's For loop is fairly straightforward -- more straightforward, perhaps, than other languages' implementations. We define the counter variable, giving it an initial value, and then we specify an end value. After each iteration of the loop, the counter variable will be incremented, and when it reaches the end value, execution will skip to the next line after the loop. For example, let's write the first hundred numbers out to the console:
For i AsInteger= 1 To100
Console.WriteLine(i)
Next
There's not much to say about the above code. Again, it's fairly straightforward. One more thing we can do with the For loop is specify the step. The step is simply the value that the counter is incremented by after each iteration. For example, we can modify the above code to display only odd numbers, which is done by specifying a step of two:
For i AsInteger= 1 To100 Step2
Console.WriteLine(i)
Next
The step doesn't have to be positive, either. If we want, instead of incrementing the counter, we can decrement it by simply specifying a negative step. Let's count backward from one hundred to one:
For i AsInteger= 100 To1 Step-1
Console.WriteLine(i)
Next
So far, we've defined the counter as an Integer . However, it's possible to use any object that supports greater-than comparison, less-than comparison, addition and subtraction. For example, let's make our counter a Double:
The final loop we'll be working with is the For Each loop. It's extraordinarily useful and implementations of it can be found in many other languages. The loop iterates over every element in a given group. So, we could iterate over all of the items in an array or in a collection. However, we haven't yet covered either arrays or collections, and so before we delve into the For Each loop, let's take a short detour (our last before we begin, I promise!).
We'll start with arrays. As in any other language, they aren't too complex and so we won't need to spend a lot of time on them. After all, an array is simply a series of objects of the same type. There are two ways of declaring an array in Visual Basic, though declaring an array is very similar to declaring a regular variable. Using these two methods, let's create two arrays of Integer s:
Dim array1() AsInteger
Dim array2 AsInteger()
As you can see, the two methods of array declaration differ only by placement of the parentheses. However, the first method is the preferred method because we can specify array bounds within the parentheses. Let's give the first array an upper bound of five:
Dim array1(5) AsInteger
If you were to try this with the other method, you'd be given an error. Also, note that upper bound is not length. Instead, it's the highest index we can work with.
We can now initialize our arrays. Since we've assigned our first array an upper bound, we can assign each element individually using the element's zero-based index:
array1(0) = 1
array1(1) = 2
array1(2) = 3
array1(3) = 4
array1(4) = 5
array1(5) = 6
We can also make the assignment on a single line, and we can do this without having first specified an upper bound. So, let's populate the second array:
As with other variables, though, we can both create and initialize an array in the same step:
Dim array3() AsInteger= NewInteger() {1, 2, 3}
We can shorten this further still. Notice how Integer appears twice in the above line. This is a bit redundant, and we can cut out the second appearance entirely:
If we need to change the dimensions of an array at any time, we can use the ReDim statement:
ReDim array2(3)
By default, though, this will delete the existing elements of the array. In order to preserve the existing elements, we need to use the Preserve keyword:
ReDimPreservearray2(3)
And, of course, we're not limited to arrays of Integer s:
Dim names() AsString= {"Bob", "Henry", "Tom"}
In order to get the length of an array, we can use the Length property:
Console.WriteLine("Length: {0}", array1.Length)
We can also get the upper bound of the array using the GetUpperBound method. This method takes one argument, which is an Integer representing the dimension whose upper bound you want to determine. This starts at zero. Since all of our arrays only have one dimension, we can pass zero:
That, of course, brings us to multidimensional arrays. Arrays can have more than one dimension. We simply need to specify this when declaring an array. Inside of the parentheses, where we set the upper bound, we actually set the upper bound for each of the array's dimensions. Let's create a two-dimensional array with two elements in each dimension:
Dim twoByTwo(1, 1) AsInteger
When we assign to it, we just need to consider both dimensions:
twoByTwo(0, 1) = 3
If we don't want to specify the upper bound of each dimension, then we omit each, but we still need the proper amount of commas. Here, we create a three-dimensional array, but we don't specify any upper bounds:
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 AsNewList(OfInteger)
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:
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 AsNewDictionary(OfString, 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.
Now we can move on to the For Each loop. The For Each Loop, as noted before, iterates over all of the elements in a given group. We could iterate over all of the elements of an array or a collection. Let's jump right into an example. Let's create an array, populate it, and then write each element out to the screen:
Dim numbers() AsInteger= {1, 3, 5, 7, 9}
ForEachnumber AsIntegerInnumbers
Console.WriteLine(number)
Next
In the above example, we create and initialize an array of numbers in one line. We then enter into a For Each loop where we create a variable, "number," that represents an element of the array. The value of this variable is, of course, updated before each iteration.
We can loop through a List(T) the same way:
Dim moreNumbers AsNewList(OfInteger)
moreNumbers.Add(1)
moreNumbers.Add(3)
moreNumbers.Add(5)
ForEachnumber AsIntegerInmoreNumbers
Console.WriteLine(number)
Next
A Dictionary (TKey, TValue), however, must be treated differently, since we have both a key and a value that we need to iterate over. In order to loop over both, we need to treat both as a single KeyValuePair (Of TKey, TValue), which is a generic class used to represent both a key and a value. Let's loop over names matched to phone numbers:
Dim phoneNumbers AsNewDictionary(OfString, String)
As you can see, the KeyValuePair (Of TKey, TValue) class allows us to access both the key and the value. If, however, we do not need both the key and the value, we can just loop over one or the other:
' Display all names
ForEachname AsStringInphoneNumbers.Keys()
Console.WriteLine(name)
Next
' Display all phone numbers
ForEachnumber AsStringInphoneNumbers.Values()
Console.WriteLine(number)
Next
Now that we've looked at all of the loops offered in Visual Basic, we can begin constructing the game.