Dynamic Arrays

Creating a Dynamic Array

As we saw last lesson, arrays are quite handy for working with lists of numbers, especially if you process them with For…Next loops.

However, what if the user wants to decide how many temperatures to enter, then what?

VB handles this by using a special elastic container called a dynamic array.

 

Creating a Dynamic Array

Dynamic Arrays are dimensioned at runtime, either when the user specifies the size of the array or when logic you add to the program determines an array size based on specific conditions.

Dimensioning a dynamic array takes several steps because although the size is not yet determined at runtime you need to allocate space for it at design time.

Creating a Dynamic Array

To create a dynamic array, you follow these 4 steps:

Specify the name and type of array in the program at design time, omitting the number of elements in the array. For example:

Public Temperatures ( ) as Variant

 

Add code to determine the number of elements that should be in the array at runtime. You can prompt the user or use other logic to calculate this.

Days=InputBox ("How many days?", "Create Array")

 

Creating a Dynamic Array

Use the variable in a ReDim statement to dimension the array.

ReDim Temperatures (Days)

This uses the Days variable from user input to set the array.

Use the number as the upper bound in a For…Next loop to process the array element, if necessary.

For I%=1 to Days

temperatures (I%)=InputBox (Prompt$, Title$)

Next i

 

Program Example

Let’s use a dynamic array to modify our temperature program.

First we need to remove the number 7 from the array declaration statement in our module.

Then let’s add the following public variable declaration to the standard module:

Public Days As Integer

 

Program Example

Next we need to alter our Enter Temperatures button code.

Cls

Days = InputBox("How many days?", "Create Array")

If Days > 0 Then ReDim Temperatures(Days)

Prompt$ = "Enter the high temperature."

For i% = 1 To Days

Title$ = "Day " & i%

Temperatures(i%) = InputBox(Prompt$, Title$)

Next i%

 

Program Example

The second and third lines of the code prompt the user for the number of temperatures he or she wants to save, and then they use the input to dimension a dynamic array.

The If…Then statement is used to verify that the number of days is greater than 0. (Dimensioning an array with the number 0 will cause a runtime error.)

The Days variable is also used as the upper bound of the For…Next loop.

 

Program Example

Next we need to modify the Display temperatures button code.

Print "High temperatures:"

Print

For i% = 1 To Days

Print "Day "; i%, Temperatures(i%)

Total! = Total! + Temperatures(i%)

Next i%

Print

Print "Average high temperature: "; Total! / Days

 

Program Example

The variable Days replaces the number 7 twice in the event procedure.

Now we can run the program and see that first it asks us for the number of days we will enter, which sets up the array and allows us to enter the temperatures.

What we have learned is how to store an unlimited number of values and process them using a For…Next loop.

 

Multidimensional Arrays

Multidimensional Arrays

We have used one dimensional arrays in the last example, but more complex programming use two, three and even four dimensional arrays--Especially game programming and scientific applications.

This next example we will use a two-dimensional array to create a baseball scoreboard.

 

Multidimensional Arrays

First let’s design the form. Here we are creating two input boxes and drawing on rectangle and one line to divide the scores.

We also have two command buttons one labeled "next inning" and the other "Quit."

 

Multidimensional Arrays

First let’s add the public module for handling the array.

Option Base 1 'set array base to 1

Public Scoreboard(2, 9) As Variant

Public Inning As Integer

Next let’s write the public procedure for adding up the scores--we will call the procedure AddUpScores.

Sub AddUpScores()

'AddUpScores is a public procedure that totals and

'displays the runs in the Scoreboard array.

For i% = 1 To 9 'use loop to add scores

AwayScore% = AwayScore% + Scoreboard(1, i%)

HomeScore% = HomeScore% + Scoreboard(2, i%)

Next i% 'then display scores in box

Form1.CurrentX = 5000

Form1.CurrentY = 1050

Form1.Print AwayScore%

Form1.CurrentX = 5000

Form1.CurrentY = 1400

Form1.Print HomeScore%

End Sub

 

Multidimensional Arrays

AddUpScores is used to sum the scores in the array and to display them. Because we have declared it in a standard module, it is available throughout the program.

Next let’s write the next inning code.

'put scores in the array each inning

Scoreboard(1, Inning) = txtAway.Text

Scoreboard(2, Inning) = txtHome.Text

'then display the scores in the score box

'(CurrentX and CurrentY control the cursor)

CurrentX = 2626 + (Inning * 224)

CurrentY = 1050

Print txtAway.Text

CurrentX = 2626 + (Inning * 224)

CurrentY = 1400

Print txtHome.Text

'change to the next inning

Inning = Inning + 1

'and if the game is over, display the results

If Inning > 9 Then

cmdNextInning.Enabled = False

AddUpScores 'this procedure (in BASEBALL.BAS)

Else 'calculates the score

lblInning.Caption = "Inning " & Inning & " Scores"

End If

 

Multidimensional Arrays

The event procedure associated with this button handles the task of moving scores from the team text boxes to the Scoreboard array and finally to the scoreboard on the form.

Each scoreboard array assignment requires two index values, one for the row and one for the column.

 

Multidimensional Arrays

The CurrentX and CurrentY are properties that determine the coordinates of the cursor on the form, and they require values in twips.

To measure twips you calculate that there are 1440 twips to an inch or about 2.5cm.

 

Multidimensional Arrays

We also need to write a procedure for the Form_load procedure.

Private Sub Form_Load()

Inning = 1 'initialize Inning to 1

CurrentX = 2850 'place cursor at top of box

CurrentY = 750

Show 'enable output during load and print header

Print "1 2 3 4 5 6 7 8 9";

Print " Final"

 

Multidimensional Arrays

The Form_load event procedure initializes the Inning variable to 1 and displays the header on the scoreboard.

The Show method is required in this procedure before the Print method is used.

Normally we would not need the show method when using the Print method, but in this case the form is still loading.