Visual Basic 2010 (Console) Guide
When we want to use a variable in a program, we must first declare the variable.
When programming, it is often necessary to store information so that it can be used at a later time in a program. In low level languages, the programmer specifies the exact location (in memory or in a particular register of the processor) where the information is stored. In high level languages, we use variables to represent stored information.
Imagine a series of boxes, each one with a label on the front to name the box. These names are called variables in Visual Basic. The contents of each box is the information stored in the variable.
It is useful to give variables meaningful names that can be easily understood by the programmer (you).
There are rules about variable names in Visual Basic.
The name that you give to a variable should help you to remember what the variable was to be used for. In Visual Basic, it is usual to use camelCase when writing variable names. A variable name must be a single word with no spaces. It should start with a lower case letter. If two or more words are combined together into a single variable name, we captialize the first letter of each word after the first. For example,
numberOfEntries = 5
wholeNumber1 = 7
The following words are used in the Visual Basic language and cannot be used as variable names.
Built-In Data Types
The following lines are examples of variable declarations in Visual Basic. The keyword Dim is short for dimension and is used when we want a new variable to be declared. The second example shows that we can give our variable a starting value. This is called initializing the variable.
Dim myWholeNumber As Integer
Dim myWholeNumber As Integer = 5
|Data Type||Description||Memory Allocated||Range|
|Integer||Signed 32-bit integer||4 bytes||-2147483648 to 2147483647|
|Byte||Unsigned 8-bit integer||1 byte||0 to 255|
|Long||Signed 64-bit integer||8 bytes||-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|Double||Double-precision floating-point number||8 bytes||Precise to about 15-16 digits|
|Decimal||Suitable for monetary calculations||16 bytes||Precise to 28-29 significant digits|
|Char||Stores a single Unicode character.||2 bytes|
|String||Strings are enclosed in double quotes.||As required|
|Boolean||Boolean value (either true or false)|
There are more built-in data types but these should do us for now.
A constant is a programmer-specified value that does not change throughout the lifetime of the program. Think of it as a variable that you cannot change. To declare a constant in Visual Basic, you do the following,
Const pi As Double = 3.141593;
Notice that you use the keyword, Const and still specify a data type.
Local & Global Variables - Scope
The scope of a variable determines which parts of the program can access its values. The variables in the program on the next page are all local variables. That means that they can only be used within the block of code where they are declared.
Global variables are declared for use throughout the program. We will meet more of these later. It is not always desirable to use global variables in complex programs since it can be easy to change the value by accident.