Visual Basic/Arrays

From Wikibooks, open books for an open world
< Visual Basic
Jump to: navigation, search

Contents

[edit] Arrays and Matrices Introduction

Arrays are extremely useful in Visual Basic, and are present in many other programming languages. Arrays are used to group similar data together, to make it easier to search and sort through this data. The best way to look at an array is to show you one. Say you want to make a phone book for your computer. Rather than make a new variable with a new name whenever you want to add a name, you can make an array. Arrays consist of the variable name and a subscript, which is put in parentheses for computer use. So, if you want to make a phone book of say, 100 people, rather than make a bunch of variables named 'person1, person2, person3....person100', you may use an array. All you have to do is dimension the array (see above). So now those 100 variables turn into person(1 to 100) in a dim statement. Now you may be asking, what do I do now? Each object stored in an array is called an element. What you have done is created an array which can hold 100 elements. If you want to print the 32nd person in your phone book, you simply would type:

 Debug.Print person(32)

[edit] Use of Arrays

Arrays have far more uses other than just making phone books. But before we get into specific uses, let's look at some of the basic techniques used when working with arrays. The first thing you may want to do is learn how to easily fill an array. Filling an array with random numbers is a popular technique for starting off programs, and testing other techniques such as sorting. To fill an array, a simple For Loop may be used.

  Option Explicit
  Dim lngIndex as Long
  Dim strArray(0 to 9) as String ' Create an array with 10 values
  Dim intCounter as Integer
  intCounter = 1
  For lngIndex = LBound(strArray) to UBound(strArray)
     intCounter = intCounter + 1
     strArray(lngIndex) = intCounter
  Next lngIndex

[edit] Dynamic Arrays

Arrays are more flexible than you think. Say you have a phone book program running and it has an array of your friends' names, like this:

  Option Explicit
  Dim StrNames(1 to 3) As String
  StrNames(1) = "Alec"
  StrNames(2) = "Jack"
  StrNames(3) = "Pie"

But say I want to add MORE friends to your phone book, when a button is clicked, for example. What do I do?! Don't worry, arrays are more flexible than you think! You can define a dynamic array instead:

 Dim names() As String

This array initially has no elements. You can add more elements, by using the ReDim keyword:

 ReDim names(1 to 2) As String

This would create two new elements. You can assign names to these elements in the same way as before:

 names(1) = "Alec"
 names(2) = "Jack"

If you want to add more entries then use, for example:

 ReDim names(1 to 3) As String

But you'll end up losing all the old records! If you want to keep them, you must also use the Preserve keyword:

 ReDim Preserve names(1 to 3) As String
 names(3) = "Eugene H / Orage" 'Whoa this person's name is too long to remember

Now before we go on, it's important to learn about some built-in functions related to arrays first: UBound and LBound. UBound is an abbreviation which stands for "Upper Bound" and LBound for "Lower Bound". Every type of array has an upper and a lower bound. In this case, our upper bound for "names" is 3 and the lower bound is 1 (see the definition above). However, those functions are not made to help you with your short term memory problem. Although you definitely can use those to check what the LBound and the UBound are if you forget. Anyway those functions really help you out when using dynamic arrays, where the bounds of an array may change at run-time. In the following example, we will print whatever's in our phone book using both functions:

 For i = LBound(names) To UBound(names) 'This statement is equivalent to "For i=1 to 3"
     Debug.Print names(i)
 Next i

Will print:

 Alec
 Jack
 Eugene H / Orage

Now, to add more flexibility with ReDim? Use the following code to add people: (This code can be placed inside a command button event so that new friends can be added when the button is clicked)

 ReDim Preserve names(1 To UBound(names) + 1) As String
 names(UBound(names)) = "my new friend"

It is worth noting at this point that the LBound and UBound functions do not work with an un-initialized dynamic array. The following would cause a run-time error:

 Dim my_array() As String
 Debug.Print UBound(my_array)

How can you avoid this run-time error?


Using the above code that dynamically adds a new member would not work. Instead a flag has to be kept:

 Dim array_is_empty As Boolean
 Dim my_array() As String
 
 array_is_empty = TRUE
 
 If Not array_is_empty Then
     ReDim Preserve my_array(1 To UBound(my_array) + 1) As String
 Else
     ReDim Preserve names(1 To 1) As String
     array_is_empty = FALSE
 End If
 names(UBound(names)) = "my new friend"

There is also a slightly obscure but quite neat hack to check if a dynamic array has been initialized.

 Dim my_array() As String
 If (Not my_array) = -1 Then
     MsgBox "Array is uninitialised."
 End If

This rather C-like construct feels like it shouldn't work in VB, but it does.

[edit] Variant Arrays

Dynamic arrays can be handled more efficiently by using the Variant type. You can declare an array like this:

 Dim variant_array As Variant
 
 variant_array = Array()

This works in a way analagous to that for conventional dynamic arrays in the previous section. You can add members in a similar way:

 ReDim variant_array(1 to 2) As Variant
 
 variant_array(1) = "John"
 variant_array(2) = "Janet"

The main difference between Variant arrays and conventional arrays is the LBound and UBound functions work with an un-initialized Variant array:

 Dim variant_array As Variant
 
 variant_array = Array()
 
 Debug.Print LBound(variant_array)
 Debug.Print UBound(variant_array)

This would display 0 and -1 respectively. This can be used to test if a Variant array is unitialized, by knowing that the upper bound is less than the lower bound (as either of the bounds can be negative). Also this simplifies the code for adding new members, as a check for an un-initialized array does not have to be carried out:

 ReDim Preserve variant_array(1 To UBound(variant_array) + 1) As Variant
 variant_array(UBound(variant_array)) = "Jennifer"

Note that for very large arrays, the overhead of using Variants may well be restrictive in which case conventional arrays should be used.

[edit] Multi-Dimensional Arrays

Arrays can be defined to have any number of dimensions (or indices), by listing the sizes of each dimension:

 Dim funky_array(2,3,1,4) As String

An element can be referenced like this:

 funky_array(1,2,0,3) = 24

Dynamic arrays can also be re-dimensioned to have any number of dimensions:

 Dim very_funky_array() as String
 
 ReDim very_funky_array(2,2,2) As String


The LBound and UBound functions can be used to find the bounds of a particular dimension:

 Debug.Print UBound(funky_array, 4)

Would give 4.

Using UBound without the second parameter gives the first dimension

 Debug.Print UBound(funky_array)
 // Displays 2
 Debug.Print UBound(very_funky_array)
 // Displays 2

[edit] Erasing Arrays

Any type of array can be re-set to empty by using:

 Erase some_array

[edit] Mixing Arrays

The real power of arrays comes when defining arrays of arrays. What does this mean? Declare an array:

 Dim varray() As Variant
 
 varray = Array()
 
 ReDim varray(1) As Variant
 
 varray(0) = Array(1,2,3)
 varray(1) = Array(4,5,6)

What do we have here? Essentially two arrays inside another. They can be referenced like this:

 Debug.Print varray(0)(2) 

Would show 3.

You can nest arrays like this to any depth and in any order and they can be of any size. A note of caution must be taken when using the ReDim statement however. You can not specifically re-dimension a particular dimension of an array, instead you need to temporarily copy to a variant.

 Dim vtemp As Variant
 
 vtemp = varray(0)
  
 ReDim vtemp(1+UBound(vtemp)) As Variant
 vtemp(UBound(vtemp)) = 7
 
 varray(0) = vtemp

[edit] Use of Matrices

Matrices are not as commonly used as arrays, but are an important element of programming. Rather than just one dimension, a matrix may have 2 or more. So, to make a matrix, the Dim statement would be: Dim m(1 to 10,1 to 10) as integer This will create a matrix that is, 10 by 10, and comprised of integers. In reality, a matrix is much like an array of arrays. The first thing you'll want to do is know how to create a loop for reading and filling a matrix. But before that, even, you should know how matrices are structured. An example matrix would be:

     1 2 3 4 5 6 7 8 9 
    
  1  1 1 1 1 1 1 1 1 1 
  2  1 1 1 1 1 1 1 1 1 
  3  1 1 1 1 1 1 1 1 1 
  4  1 1 1 1 1 1 1 1 1


This is a 4 by 9 matrix. When talking about matrices, rows are always stated before columns. You will also notice column numbers travel from left to right, while row numbers travel from top to bottom. This is very important. And of course, matrices need not consist of merely 1s. So, now you want to fill a matrix? Its very similar to an array.

 For r = 1 To 4
     For c = 1 To 9
         m(r,c)=1
     Next
 Next

This will of course fill the matrix with nothing but ones, but random numbers may be used (see references). As you may notice, this involves nested loops. The loop fills left to right, top to bottom (the same directions we use when reading).

Previous: Strings Contents Next: Files
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export