· how arrays can be used to store sets of related data
Variables
You can't do much with programs that just use literal values all the time. What if we have a program that has to get some user input - say a number - and we need to do something with this number? If we don't know what the number is going to be in advance, we need something in our code that will represent this unknown number. That's where variables come in.
So what is a variable?
a variable can hold information for us
a variable can hold a value given by the user (from user input)
a variable can change it's value
Now, every variable in a program is allocated it's own space in memory. You can imagine variables as boxes (memory locations), where...
The box has a name, (the name we give our variable)
The box holds a value, (the value of the variable)
Here I have a variable called a which has a value of 1000 and a variable b with a value of 40 and c which does not have a value yet. Perhaps the variable c will have an unknown value until the user runs the code and we get some user input.
Declaring Variables
In Visual Basic you should declare your variables first. This is relatively straightforward. Let's declare the variables, a, b and c.
Dim a
Dim b
Dim c
I have just declared my three variables, a, b and c. It's as simple as that. There is one one problem though. I have not told VB what type of variables a, b and c are going to be. So VB assumes a kind of worst-case scenario and allocates a lot of memory for these variables. It does this in case I set the variables to hold BIG values later on in the code.
Datatypes
To stop VB allocating too much memory for my variables, I just need to tell VB what type of data my variables are going to store and then it can allocate much less memory for them.
To understand types of data (datatypes), imagine you are filling in a form. Perhaps you have to type in your name, your age and your bank balance. The information or data you are entering is of different types. Your age is a number, while your name is a sequence of characters. Also, your bank balance may be a much bigger number than your age, (hopefully anyway.)
The table below shows some of the different datatypes used by VB.
I have now explained that it is best to specify a datatype when declaring variables. However, I haven't shown you how to do this yet. Well, it is quite straightforward. Instead of just declaring variables like this...
Dim a
Dim b
Dim c
We now include the datatype in the declaration, like this:-.
Dim a As Byte'this variable can hold a whole number from 0 to 255
Dim b As Integer 'this variable can hold a bigger whole number
Dim c As Single 'this variable can hold a fairly big decimal number
~~Activity~~
Activity B
Can you remember the different datatypes?
Find out how many datatypes you can remember with this Flip Card Activity
Arrays
Most of the datatypes we have seen so far, hold one single value. But what if we have lots of values, which are all related, and we want to keep them together in a set? To keep a set of data together, we can use a data structure called an Array.
Example 1 - Array of Integers
Suppose for instance, you want a collection of three integers. Then you would declare an array of integers as follows:-
Dim anArray(3) As Integer
This creates an array with 3 elements, with the elements numbered 0 to 2. Each element can store an integer value. The name of this particular array is anArray, but it is up to you to choose any name you please.
Here's a diagram of my array. The array elements are numbered starting from 0. You may think it is strange to start numbering array elements using 0, but lots of programming languages do this. It is possible to tell VB to number array elements from 1 but I will not explain that here.
Element Number
0
1
2
Value
0
0
0
Perhaps you are wondering why all the values in each element are 0. Well, by default, VB assigns the value of 0 to integers.
If I want to change the values in the array I just do this:-
anArray(0) = 100
anArray(1) = 200
anArray(2) = 300
My array will now look like this.
Element Number
0
1
2
Value
100
200
300
If I want to use any of the values in the array I can do something like this:-
Dim sum As Integer
sum = anArray(0) + anArray(1) = anArray(2) 'add all the array values
sum = anArray(0) - anArray(2) 'subtract the last array value from the first
~~Activity~~
Activity C
Now answer the following questions:-
Examine the array of integers shown below. The name of the array is numArray.
Suppose I now want a collection of four strings. Then I would declare an array of strings as follows:-
Dim strSet(5) As String
This creates an array with 5 elements, with the elements numbered 0 to 4. Each element can store a separate string. The name of this particular array is strSet, but it is up to you to choose any name you please.
I have given the following values to the elements in the array.
You should be able to figure out what the message says yourself.
~~Activity~~
Activity D
Array Program Example
Let's create a simple program that displays array values in a listbox.
Open Visual Basic.
Save the new form and project to a new folder called Array_Example
Add a command button and a listbox to your form. Change the button's caption to 'Show Array Values' and the button's name to cmdShow. Your form should look similar to my form which is shown above.
Copy the code below into the form's coding window.
Run the program and click the button to see the results.
Now add code so that the array stores even more alphabet letters, say a to h. Make the listbox those extra letters too.
The code is between the dotted lines below
.....................................................
Private Sub cmdShow_Click() Dim charArray(0 To 25) As String
charArray(0) = "a"
charArray(1) = "b"
charArray(2) = "c"
charArray(3) = "d"
ListBox1.Clear
ListBox1.AddItem "element 0 has a value of " & charArray(0)
ListBox1.AddItem "element 1 has a value of " & charArray(1)
ListBox1.AddItem "element 2 has a value of " & charArray(2)
ListBox1.AddItem "element 3 has a value of " & charArray(3) End Sub
Although the code above works fairly well, there is a much more efficient way of writing the code, using a loop.
Delete all of your previous code and add the code shown below instead.
Run the program and click the button to see the results.
The code is between the dotted lines below
.....................................................
Private Sub cmdShow_Click() Dim charArray(0 To 25) As String Dim i As Integer
For i = 0 To UBound(charArray)
charArray(i) = Chr(i + 97)
ListBox1.AddItem "element " & i & " has a value of " & charArray(i)
Next End Sub
Do you think this code is more efficient? Imagine if you had hundreds of elements in your array and had to display the value of each element. Which code would you prefer to use, the first code or the looping code? Note:
You will find out about loops properly in the next tutorial.