Working with Loops, Arrays, and Collections in VBNET Game Development - For Each...Next
(Page 2 of 4 )
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() As Integer
Dim array2 As Integer ()
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) As Integer
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:
array2 = New Integer () {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
As with other variables, though, we can both create and initialize an array in the same step:
Dim array3() As Integer = New Integer () {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:
Dim array3() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
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:
ReDim Preserve array2(3)
And, of course, we're not limited to arrays of Integer s:
Dim names() As String = { "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:
Console.WriteLine("Upper bound (first dimension): {0}", array1.GetUpperBound(0))
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) As Integer
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:
Dim threeDimensional(,,) As Integer
Next: Collections >>
More Visual Basic.NET Articles
More By Peyton McCullough