Software Development

  Lecture Notes

Datatypes and Arrays

Introduction

Variables

Datatypes

Arrays


Introduction

Objectives

This guide will help you to understand: -  

·  what variables and datatypes are

·  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.

    Type
Description
    Range of Values

Integer Types

   Byte

small whole numbers    0 to 255

   Integer

larger whole numbers    -32,768 to 32,767

   Long

large whole numbers    –2,147,483,648 to 2,147,483,642.

Real Types

   Single

decimal numbers    +/- 3.4 E38 to +/- 1.401298e-45

   Double

larger decimal numbers    +/- 1.8 E308 to +/-2.2 E-308

Other Types

   Boolean Can hold one of two values e.g. True or False
   Date Holds a date value e.g. January 1 1999
   String a set of characters e.g. "cat"
   Variant an undeclared variable Holds any type but is 16 bytes long
  
............................................................................................................................................................................

 

Now look at the values below, which datatype would you say each one was?

1     -2     0.3     cat     5000     30000000000     TRUE     -0.000000043167     1.2 E308

 

~Now try this activity~

Activity A

1. A variable is going to hold the following value:-

200

Which datatype would be the most appropriate?

Byte
Integer
Long
Single

2. A variable is going to hold the following value:-

-0.00005

Which datatype would be the most appropriate?

Byte
Integer
Single
Double

3. Examine the variable declaration below;-

Dim b as Integer

Which one of the following lines assigns an inappropriate value to the b variable?

b = 32000
b = -30000
b = 0
b = 400.3

Score =
Correct answers:

 

Declaring Variables Properly

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:- 

  1. Examine the array of integers shown below. The name of the array is numArray.

    Element Number
    0
    1
    2
    Value
    10
    50
    -4

    How would you add the elements 0 and 2 together?

    Ans


    What is the resulting number when you add elements 0 and 2 together?

    Ans


  2. Examine the array of singles as shown below. The name of the array is sArray.

    Element Number
    0
    1
    2
    Value
    10.2
    3.0
    -60.1

    How would you multiply the elements 0 and 1 together?

    Ans


    What is the resulting number when you multiply elements 0 and 1 together?

    Ans
 

 

Example 2 - Array of Strings

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.

strSet(0) = "hi"

strSet(1) = "you"

strSet(2) = "today"

strSet(3) = "are"

strSet(4) = "how"

Here is a diagram of my array.
  

Element Number
0
1
2
3
4
Value
hi
you
today
are
how
 

Let's make a sentence out of the array elements.

Dim msg As String

msg = strSet(0) & strSet(4) & strSet(3) & strSet(1) & strSet(2)

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.

  1. Open Visual Basic.
  2. Save the new form and project to a new folder called Array_Example
  3. 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.
  4. Copy the code below into the form's coding window.
  5. Run the program and click the button to see the results.
  6. 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

.....................................................

Extension Work:

  1. Although the code above works fairly well, there is a much more efficient way of writing the code, using a loop.
  2. Delete all of your previous code and add the code shown below instead.
  3. 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.

 


Fini
 

 

 

   

  Unit Information

Assessment

Syllabus

Scheme of Work

Notes &Tutorials

Assignments

Quizzes

Books & Things

Links