Home and Learn: VB Net Course

Assigning Values to an Array

This lesson is part of an ongoing tutorial. This first part is here: What is an Array?

There are a number of way you can put data into each position of an array. The code we just wrote for the two buttons had known values stored into each position. We knew we that we wanted the numbers 1 to 5 to be stored into our Integer array, and we knew that we wanted the text "This is a String Array" stored into our String array.

But you don't have to know what the values are. You can assign values straight from a Textbox into the position of your array. Like this:

MyNumbers(0) = Val(Textbox1.Text)
MyNumbers(1) = Val(Textbox2.Text)

With that code, whatever you typed into the Textboxes on your Form would be stored into the positions of your array. The same would be true of a String Array:

MyNumbers(0) = Textbox1.Text
MyNumbers(1) = Textbox2.Text

But do we have to keep typing out a value for each and every position of our array. What if we had an array with a hundred items in it, MyText(99)? Would we have to type out text for all one hundred positions of the array?

Well, obviously not. You can use code to assign values to your array. Here is an example where we don't type out values for all positions of an array. It's the times table again. This time we'll use an array. And we'll write a line of code to assign values to each position of the array.

  1. First, add another Button to your form.
  2. Set the Text Property to "Times TableArray"
  3. Add a Textbox to your Form
  4. Set the Text Property to a blank string (in other words, delete Textbox1 from the Text property)
  5. Add a Label near the Textbox
  6. Set the Text property of the Label to "Which Times Table do you want?"
  7. Now double click your new button to get at the code window. Add the following code:

Dim numbers(10) As Integer
Dim times As Integer
Dim StoreAnswer As Integer
Dim i As Integer


times = Val(TextBox1.Text)

For i = 1 To 10

StoreAnswer = i * times

numbers(i) = StoreAnswer
ListBox1.Items.Add(times & " times " & i & " = " & numbers(i))

Next i

Run the programme. Enter a number in your new text box, and then click the Times Table Array button. The times table for the number should have been printed.

No more reading these lessons online - get the eBook here!

At the top of the code we set up three normal Integer variables, i, times and StoreAnswer. (We didn't really need the StoreAnswer variable, but it is here to make the code more readable.) We also set up an array. (Notice that we set it to 10. This actually gives us 11 positions in the array. But we're only putting something in positions 1 to 10. This is because it is more convenient for us, and for our Loop.)

Dim numbers(10) As Integer

We need to know what number we are going to be multiplying by, which times table we're working out. We get this number from the Textbox, and can assign it directly to the variable times

times = Val(Textbox1.Text)

We can then set up a For Loop. Inside the For Loop is where we'll assign values to each position of our array:

numbers(i) = StoreAnswer

First time around the loop, the variable i will hold a value of 1. So the second position of our array, numbers(1) will be assigned whatever is in the variable StoreAnswer

The second time around the loop, the variable i will hold a value of 2. So the second position of our array, numbers(2), will again be assigned whatever is in the variable StoreAnswer

We go round and round the loop assigning values to all ten positions of our array.

The other two lines of code inside the array just work out the times tables, and Adds the answer to the List Box. Study them, and make sure you understand how they work.

But the point of this is to demonstrate that you can use code to assign a value to a position in an array.

In the next part, we'll take a look at situations where you don't know how many items will be in an array.

Back to the VB NET Contents Page


Buy the Book of this Course

Email us: enquiry at homeandlearn.co.uk