arrays. lesson objectives to understand what an array is and it’s function to know how code and...
TRANSCRIPT
Arrays
Lesson Objectives
To understand what an array is and it’s function
To know how code and array in VB
The Basics
So we know what a variable is and how it can store one piece of information at one time, bit like a box
You can insert something into the box.
‘name’
You can find out whats in the box by referencing
The Problem
The downside to this is that you have to declare lots of variable if you want to store lots of information
To the rescue
There’s a way to get round this, and that’s arrays
An array in its simplest form is a variable that has lots of slots in which you can store information, a bit like pigeonholes
So the whole thing is an array with the slots being areas in which you can use
How does it work in VB
So just like variables you have to declare the array and also specify the type of the array
Dim myArray(10) As String
Assigning to the array
myArray(0) = ‘Hello’
Referencing the array
myVar = myArray(0)
Don’t forget that the
indexes start at o (zero)
This means that if you set up an array with 5 slots the first
one is accessed using myArray(0)
The number of slots you need
Multidimensional Arrays
There are times in which it would be useful to sort more information, for example storing information about a set of people.
0 1 2 3 4 5 6
Single-Dimensional Array
0, 0 1, 0 2, 0 3, 0 4, 0 5, 0 6, 0
0, 1 1, 1 2, 1 3, 1 4, 1 5, 1 6, 1
0, 2 1, 2 2, 2 3, 2 4, 2 5, 2 6, 2
0, 3 1, 3 2, 3 3, 3 4, 3 5, 3 6, 3
0, 4 1, 4 2, 4 3, 4 4, 4 5, 4 6, 4
Multi-Dimensional ArrayFirst Dimension
Second Dimension
Multidimensional Arrays
Dim myArray(3, 2) As Integer
Assigning to the array
myArray(0, 1) = 132
Referencing the array
myVar = myArray(0, 0)
The same principles apply to a multidimensional array as they do to a single-dimension array
Tom James Pete John
132 40 75 83
4 3 4 5
Challenges
Bronze Challenge - Copy this program, to create and display a list of names. Each name should be printed to a new line.
Silver Challenge - Create a multi-dimensional array to store the name and star sign of a group of students. Display a details for a random person when the program is run.
Gold Challenge - Use a multi-dimensional array to store names & scores for these Strictly Come Dancing results.