Visual Basic 2010 (Console) Guide
Data Types

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.

Variable Names

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

Keywords

The following words are used in the Visual Basic language and cannot be used as variable names.

AddHandler AddressOf Alias And
AndAlso As Boolean ByRef
Byte ByVal Call Case
Catch CBool CByte CChar
CDate CDec CDbl Char
CInt Class CLng CObj
Const Continue CSByte CShort
CSng CStr CType CUInt
CULng CUShort Date Decimal
Declare Default Delegate Dim
DirectCast Do Double Each
Else ElseIf End EndIf
Enum Erase Error Event
Exit False Finally For
Friend Function Get GetType
Global GoSub GoTo Handles
If Implements Imports In
Inherits Integer Interface Is
IsNot Let Lib Like
Long Loop Me Mod
Module MustInherit MustOverride MyBase
MyClass Namespace Narrowing New
Next Not Nothing NotInheritable
NotOverridable Object Of On
Operator Option Optional Or
OrElse Overloads Overridable Overrides
ParamArray Partial Private Property
Protected Public RaiseEvent ReadOnly
ReDim REM RemoveHandler Resume
Return SByte Select Set
Shadows Shared Short Single
Static Step Stop String
Structure Sub SyncLock Then
Throw To True Try
TryCast TypeOf Variant Wend
UInteger ULong UShort Using
When While Widening With
WithEvents WriteOnly Xor #Const
#Else #ElseIf #End #If
- & &= *
*= / /= \
\= ^ ^= +
+= = -= 

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 TypeDescriptionMemory AllocatedRange
IntegerSigned 32-bit integer4 bytes-2147483648 to 2147483647
ByteUnsigned 8-bit integer1 byte0 to 255
LongSigned 64-bit integer8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
DoubleDouble-precision floating-point number8 bytesPrecise to about 15-16 digits
DecimalSuitable for monetary calculations16 bytesPrecise to 28-29 significant digits
CharStores a single Unicode character.2 bytes 
StringStrings are enclosed in double quotes.As required 
BooleanBoolean value (either true or false)  

There are more built-in data types but these should do us for now.

Declaring Constants

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.