Variables
"There are 10 kinds of people in the world today – those who understand binary and
those who don't!”
Unknown
What is a Variable (Object)?Inside your computer are various bits of
computer technology
CD-ROMHard Disk Drives RAM (Random Access Memory)
Variables allow us to control the memory, the RAM
A Simple CalculationConsider 3 = 1 + 2
A computer needs to set aside three areas of RAMOne for the result Two for the other two numbers
It will also perform the calculation in binary for example… 00000011=00000001 + 00000010 We as programmers really don’t want to get involved in
thinking about memory and binary so we use variable objects to do the dirty work for us.
Examining the Swap Offer FunctionalityOnce we have signed up and logged onto the
system we see the following options…
We shall examine the code for “Submit Offer”. To do this we shall add a break point to the code. (F9)
Declaring VariablesNotice the following lines of code…
These lines of code “declare” variables. “Dim” tells the language to set aside sections of RAM with the names we specify
DimWhat this is doing is telling the computer to allocate one or
more addresses in RAM, give it a name and limit the type of data it can hold.
We are creating a variable object so that we can control some of the RAM.
e.g.
Dim ErrMsg as String
Declares a variable called ErrMsg with the String data type.
(Dim may be thought of as Declared In Memory even though it really means Dimension.)
So What is RAM?RAM may be thought of as a series of boxes that
allow us to store data in.With these declarations
Dim ErrMsg As String
Dim OfferTitle As String
Dim Description As String
Dim SwapNo As Integer
Dim Userno As Integer
Dim EMail As String
Dim UserName As String
We have created seven boxes in RAM for storing data
Data TypesThe data type is the name of the type of data that the variable is able to store
The Integer data type only allows us to store whole numbers
For example we could store in the variable SwapNo the following values…
0, 1 200, 50 i.e. all numeric whole numbers
But not the following…
“Kilroy Wos Ere”, 3.7
because…“Kilroy Wos Ere” text not numbers3.7 not a whole number (This would work but it
would be rounded!)
Things to tryWhat happens if…
You delete the line Dim Description As String and try to run the program? (Notice the error messages and the tools to help you locate problems, use undo to fix the error.)
You rename the variable SwapNo to SwapNumber and run the program? (As before notice the error messages and then fix the error.)
You change the data type of Description from String to Integer? (You will need to use the program to see the problem.)
AssignmentWhat does the word assignment mean?
To assign something to a variable we use the assignment operator…
=So to assign the number 1 to the
FirstNumber variable we would use the following line of code…
FirstNumber = 1
The Assignment OperatorUsing the debugger (F9, F10) we shall examine how
the following highlighted line of code works.
Assigning Literal ConstantsThere are times when we simply want to give a variable a
value, often to initialise it.
A literal constant is a set value specified by you the programmer.
Take a look at the following… Dim DaysLeft As Integer DaysLeft = 365
This could also be written like so…Dim DaysLeft As Integer = 365
Performing Simple CalculationsTake a look at the following code... Dim FirstNumber As Integer Dim SecondNumber As Integer Dim Result As Integer FirstNumber = 20 SecondNumber = 30 Result = FirstNumber + SecondNumber What is happening here and what will be the value of
Result?
Finding the Definition of a VariableIf you are looking at a section of code and you see
a variable but cannot find where it is declared right click on it like so...
Data Type Conversion ErrorsThe process of assigning data unsuitable for
the specified data type is called a data type conversion error
e.g.
Dim MyAge as IntegerMyAge = “Fred Bloggs”
Overflow ErrorsDifferent data types allow different ranges
of values to be stored in themFor example the largest number a
variable with the Integer data type may store is 2147483647. Attempting to store a number larger than this will result in an overflow error.
e.g. Dim MyAge As IntegerMyAge = 2147483648
Rules for Naming your VariablesMake your Variable names Meaningful
Don’t use A B & C for your variable names
Not all characters may be used in variable namesSpaces are not allowed My Variable – bad MyVariable – good
Variables may contain a number but not start with one
Use Camel Case when naming your variablesInputVariable - goodinputvariable - bad
Summary A variable allows us to control a section of RAM which has been given
a name by you the programmer
A variable has a data type which controls what sort of data it may store. (Any attempts to assign the wrong type of data to a variable will produce an error!)
A variable must be declared before you may use it. The key word Dim is used in VB to do this
A variable has a value this value is the data stored at whatever location in RAM is used
The value of a variable is assigned using the assignment operator “=”. This may be a discrete value e.g. 1
Value=1 or the result of a calculation e.g.…
Result = FirstNumber + SecondNumber
QuestionsWrite down variable declarations that would store the following values: For example, an address:
Answer : Dim AnAddress as String 1. A person’s name 2. A person’s age 3. A telephone number 4. A date of birth 5. The price of a kettle
Questions6. Complete the following code that assigns the value 20 to a
variable called ClassSize:
Dim ClassSize as IntegerClassSize 7. Complete the following code that assigns the name “Fred
Bloggs” to a variable called StudentName: Dim StudentName as StringStudentName
Questions8. A form has a text box called txtUserName.a. Write a suitable variable declaration for a variable to store
this data: b. Write a suitable assignment statement that reads in this
value from the form:
9. Write an assignment statement that assigns the result of 2 + 2 to variable called Result:
10. Write an assignment statement that assigns the result of
4.5 x 2 to a variable called Result: