VB Data Types Variables & Constants

Data Type
A data type is a key word used to specify the kind / type of data that can be stored in a memory location (they define the type of data that a variable can be able to store). Visual Basic supports several data types as specified below:

Constants
A constant is a memory location whose data / value does not change during program execution. The value of a constant must be assigned during declaration. Syntax: const  constantname =  Some Value const constantname  As datatype  =  Some Value Example: const pie  = 3.1415 const pie  As Double = 3.1415

Variables
As objects are added to the form, Visual Basic automatically builds a framework of all event procedures. You simply add code to the event procedures you want the application to respond to.

A variable is a named memory location used by Visual Basic to hold information needed by your application. This data may change during program execution.

There are three ways for a variable to be declared:
 * 1) Default
 * 2) Implicit - declared by
 * 3) Explicit

When variables are not implicitly or explicitly typed, they are assigned the variant type by default. The variant data type is a special type that is used by Visual Basic and can contain numeric, string, or date data.

To implicitly type a variable, use the corresponding suffix as specified above in the data type table. For example,TextValue$ = "I am a string" creates a string variable, while Amount% = 500 creates an integer ariable or specify option implicit in the general declarations section. This makes VB loosely typed.

There are many advantages to explicitly typing variables. Primarily, this ensures all computations are properly done, that mistyped variable names are easily identified, and that Visual Basic takes care of insuring consistency in upper and lower case letters used in variable names. Because of these advantages, it is advisable to explicitly type all variables (either by specifying option explicit in the general declarations section. This makes VB strongly typed or by declaring variable dimensions).

To explicitly type a variable, it is important to first determine its scope. There are four levels of variable scope:
 * 1) Procedure level
 * 2) Procedure level, static
 * 3) Form and module level
 * 4) Global level

procedure level - variables are declared using the Dim statement: Dim MyNumber AS Integer Dim MyVolume AS Double Dim MyName, YourName AS String Procedure level variables declared using the dim statement do not retain their values once a procedure terminates.

procedure level, static -To make a procedure level variable retain its value upon exiting the procedure, replace the Dim keyword with Static : Static MyNumber AS Integer Static MyVolume AS Double

Form (module) level variables - they retain their values and are available to all procedures within that form (module). Form (module) level variables are declared in the generan declarations part of the object in the form (module's) code window. The Dim keyword can be used: Dim MyNumber AS Integer Dim MyVolume AS Double Dim MyName, YourName AS String

Global level variables retain their values and they are also available to all procedures / functions within an application. These variables are declared in the declarations part of the general object of a module's code window (It is advisable to keep all global variables in one module). In declaring use the Global keyword: Public MyNumber AS Integer Public MyDate AS Date

Rules used in naming variables and constants:

 * 1) No more than 40 characters
 * 2) They may include letters, numbers, and underscore (_)
 * 3) The first character must be a letter
 * 4) You cannot use a reserved word (word needed by Visual Basic)