Chapter 2 - Variables

advertisement
Chapter 2 - Variables
Variables 2.1
Introduction
We are definitely becoming a society of vast amounts of information. However this
information needs to be stored where it can be used and accessed. Our mail gets
stored in a mailbox, our E-mail gets stored in our in boxes and our personal items
hopefully get stored neatly in our rooms.
Programming requires information to be stored. The information is stored in what are
called variables. Variables are like the mailboxes of our computers. Each variable
contains information, just as a mailbox contains envelopes/letters. Each variable has an
address, just as each mailbox is assigned a street address. Each variable will have a
specific name, similarly each mailbox is labeled with the name of a family or business.
A variable in programming is information or a value that is expected to change. Days of
the year change, the temperature in our Canadian climate changes and whether we like
it or not our age changes. In programming we use variables to store information or
values that are expected to change during the course of the program.
Variables in Visual Basic hold information or values. When a variable is used or
declared the Visual Basic sets up a storage space, such as a named memory box, in
the computers memory. When needed in a program visual basic can access this
variable and its value as long as it has been declared.
Definition
Declared means when a variable is given a name.
Outcomes
At the end of this chapter
you should be able to:
1. Declare a variable
using a data type.
2. Declare a variable using all eleven data types.
3. Declare a variable and assign the variable to an object.
4. Know where to declare a variable (local, global or modular).
5. Declare more than one variable of the same data type.
6. Know the scope of variables.
7. Know the difference between a variable and a constant.
8. Declare a constant and use it in a program.
9. Know the order of operations (PEDMAS/BEDMAS).
10. Use mathematical operations in a program.
11. Use the message box where appropriate in a program.
12. Use of multiple forms in a project.
13. Know how to refer to object values or information from form to form.
Key Words
For the explanations and definitions of the key words see Appendix B
Variable Description
A variables is a named storage place in a computers memory. Variables are used in
a code statements to store temporary values used by other code statements. Code
statements are lines of programming code inside an object. For the most part variables
are declared as Option Explicit in the General Declarations area of the program (see
frmVariableDeclaration) . Variables in Visual Basic all have a name, that the
programmer creates, and a data type that Visual Basic pre-defines.
The contents of a variable determine
what data type a variable will be. A data
type is whether the contents of a variable
is numbers, letters or special numbers.
Data types will be discussed below.
The following is an example of a variable
declaration using Option Explicit in the
General Declarations section of a
program. Notice that the Object is General and the Proc is declarations.
Variable Data Types
The contents of a
variable is a
determining factor as
to what data type the
variable will be. Visual
Basic holds 11
standard data types for variables. It is also possible to define your own variable types.
For our purposes we will be using the standard data types. The most often used data
types include string and integer.
For example: Dim Num1 as Integer
Variabl Storage
e Type Size (in
bytes)
String
length
Range
1 to 65,535 characters
of string
Description
string variables hold
characters.
one of the most common uses
of strings variables is to
pick up the information
contained in a text box.
Byte
1
0 to 255
hold relatively small integer
values.
it is a no type-declaration
characters
Boolea 2
n
True or False
are no type_ declaration
characters.
used when there are logical
tests to be performed.
Integer 2
-32,768 to 32,767
hold relatively small integer
values.
used when integers require no
decimal point.
Long
4
Integer
s
-2,147,483,647 to
2,147,483,647
it is a no type-declaration
character
use whenever a number is
larger than an integer.
eg. the cost of a new house,
$150,000
Single 4
Precisi
on
-3.402823E38 to
-1.401298E-45
for negative numbers
-1.401298E-45 to
-3.402823E38
Double 8
Precisi
on
Curren 8
cy
hold relatively small integer
values.
it is a no type-declaration
character.
usually numbers that contain
decimal places.
for positive numbers
use when you do not want
integers to be rounded off.
eg. 23.45
1.79769313486232E3
08 to
4.94065645841247E324 for negative
numbers
hold relatively small integer
values.
4.94065645841247E324 to
1.79769313486232E3
08 for positive
numbers
use when you need very small
or very large numbers that
require great precision.
-922,337,203,685,477.
5808 to
2,337,203,685,477.580
7
hold relatively small integer
values.
it is a no type-declaration
character.
needs greater storage.
it is a no type-declaration
character.
used when accuracy is
important.
Date
8
Jan. 1, 100 to Dec. 31,
9999
used to find days between two
dates.
Variant 16 for
negative
numbers
22 +
string
length
Any numeric value up
to the range of a
Double
hold relatively small integer
values.
it is a no type-declaration
character
Download