As discussed before, arrays are one variable that can hold multiple objects of the same type. However, in .NET, Arrays are also viewed as their own type of object. This means they also come with their own built-in functions. For example, if you want to find the number of objects in an array, you can use:

Dim ExampleArray() As String = {"array", "strings", "here"}
Dim ElementCount As Integer
ElementCount = ExampleArray.Length

In the above example, the variable "ElementCount" would end with a value of 3. Although this is helpful for one-dimensional arrays, two-dimensional arrays may be a little more complex when obtaining the full structure. However, we can use the GetUpperBound function for this.

Dim ArrayOfNumbers() As Integer = {{2,4}, {5,3}, {5,5}}

Notice that this array has three rows and two columns. If we didn't know this, we could still figure it out by using this code:

Dim Rows As Integer
Dim Columns as Integer
Dim Rows = ArrayOfNumbers.GetUpperBound(0)
Dim Columns = ArrayOfNumbers.GetUpperBound(1)

Notice that when I use this, I pass it an integer. This integer represents the index-based dimension I am referencing. For example, I am referencing the first dimension of the array to get the rows. Next, I am referencing the second dimension of the array to get the column. An easy way to remember the rows versus the columns is to simply remember that a one-dimensional array is nothing but a row of objects. Therefore, the second dimension will ALWAYS be adding the columns to the row.

There are some other functions that are included for arrays. This includes:

  • .Length - Returns the total number of elements in all dimensions of the array as an integer.
  • .GetLowerBound - Returns the index of the first element in the array.
  • .GetUpperBound - Returns the index of the last element in the array.
  • .Clear() - Resets all of the values in the array to their initial empty values.
  • .IndexOf(Value to search for (Must be the same variable type as the array)) - Returns the index of the first matching value. Can only be used on one-dimensional arrays.
  • .LastIndexOf(Value to search for (Must be the same variable type as the array)) - Returns the index of the last matching value. Can only be used on one-dimensional arrays.
  • .Sort() - Sort a one-dimensional array. For example, integers will be sorted from least to greatest, while Strings will be sorted alphabetically.
  • .Reverse() - Sorts a one-dimensional array backwards. For example, integers will be sorted from greatest to least, while Strings will be sorted from Z to A.

Now that you have a grasp on arrays, as well as how to use many of the internal functions of Visual Basic, we will start discussing Conditional logic.

Previous Next

Startup Discount
Small Business?
Contact us for an Enormous Discount!