Introduction to Arrays

Variable Arrays

A variable array is a set of variables with the same name and the same type. Each individual variable is called an element and is identified by a unique number called an index. A string array might look like the following:

Example 1
' dimension
Dim strNames(1 to 5) As String
' assign values
strNames(1) = "Andrew"
strNames(2) = "Wilma"
strNames(3) = "Cory"
strNames(4) = "Mark"
strNames(5) = "Denise"

In Example 1, a single variable array is dimensioned with 5 elements. Each element is like a separate variable with the same name and a unique index. The lower bound (the smallest index possible) of strNames is 1, and the upper bound (the highest index dimensioned) is 5.

It is important to stay within the bounds of an array. If you try to access strNames(6) in the above example, you will receive an error message that says, “Subscript out of range.” The word subscript is another word for index.

LBound and UBound

The LBound function returns the lower bound (the smallest index) of an array. The UBound function returns the upper bound of an array.

For example, the code:

intUpper = UBound(strNames)

would set the variable intUpper to the number 5 if that were the upper bound of the variable array strNames.

ListBox Control

 

The ListBox control displays a list of items and lets the user select one or more of them. The most important properties, methods and events are as follows:

Properties

  • ListCount – the number of items in the list (read-only)
  • ListIndex – the index number of the current item (read-only)
  • MultiSelect – determines whether a user can make multiple selections
  • List – an array of the text displayed in the list box
  • ItemData – an array of special index numbers that can be assigned to list box items
  • Sorted – determines whether the list is sorted alphabetically

Methods

  • AddItem – add an item to the list
  • RemoveItem – remove an item from the list
  • Clear – clear all items from the list

Events

  • Click – occurs when the user selects an item (with mouse or keyboard)
  • DblClick – occurs when the user double-clicks on an item

Class Exercise: Name List

We will make a program that allows the user to enter five names and then displays them.

 

  1. Begin by designing the form.
  2. Use the names lstNames, lblCount, cmdDone for the controls on the form.
  3. Add code to cmdDone.
  4. Create a subroutine named GetNames that dimensions a string variable array and uses a For…Next loop twice, once to read names with InputBox, and again to display the names in the listbox. It should also display the total number of names in the label.
  5. In your Form_Load, add a call to the GetNames subroutine.

Name List Code
Private Sub cmdDone_Click()
    ' end program
    Unload frmMain
End Sub

Private Sub Form_Load()
‘ get the names
Call GetNames
End Sub

Sub GetNames()
‘ **************************************************************
‘ ** Get and display names from the user
‘ **************************************************************

‘ dimension
Dim strNames(1 To 5) As String
Dim intCount As Integer
‘ get values from user
For intCount = LBound(strNames) To UBound(strNames)
strNames(intCount) = InputBox(“Enter a name:”)
Next intCount
‘ display names
For intCount = LBound(strNames) To UBound(strNames)
Call lstNames.AddItem(strNames(intCount))
Next intCount
‘ display total
lblCount.Caption = CStr(lstNames.ListCount) & ” names”
End Sub