Understanding Visual Basic Variable and Constant Types
Previous | Table of Contents | Next |
Visual Basic Modules and Procedures | Declaring Visual Basic Variables and Constants |
Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99 |
A variable is a location in memory where a value can be stored during the execution of a Visual Basic application. Visual Basic variables are assigned names by the programmer when they are declared so that they can easily be referenced in other places in the application code.
These values may either be declared as variable (in that once the value has been assigned it can be changed later in the Visual Basic code) or as constant (in that once the value has been assigned, it cannot be changed elsewhere in the code).
In the next chapter (Declaring Visual Basic Variables and Constants) we will look at how to create Visual Basic variables and assign values to them. Before doing that, however, we need to look at the various types of variables that are available.
Boolean Variable
The Visual Basic Boolean variable type holds a value of true or false. Internally these are actually stored as the numbers 1 and 0 representing true and false respectively. Boolean values are used in conditional statements to decide whether particular parts of Visual Basic code should be executed or not (see Visual Basic Flow Control for more details on conditional statements).
Char Variable
Byte Variable
A Byte variable holds a positive number in the range 0 to 255. Because of the limited range, Byte variables should be used with caution. An attempt to assign a negative value or a value greater than 255 to a Visual Basic Byte variable will result in an error.
Date Variable
The Visual Basic Date variable type holds a date and time value. Dates are declared in the form #mm/dd/yyyy#. For example, May 23, 2007 would be declared as #5/23/2007#. Visual Basic provides a number of mechanisms for working with Date variables (such as calculating a date six months from today's date). These are covered in the Working with Dates and Times in Visual Basic section of this book.
Decimal Variable
The Decimal variable type can store either a whole number or a decimal number up to 29 decimal places. When working with whole numbers, however, it is recommended that the Integer variable type be used as this is more memory efficient than the Decimal variable type.
Double Variable
The Visual Basic Double variable is used to store either very large numbers, or small numbers requiring more than 28 decimal places. To be precise Visual basic Double variables can store positive numbers in the range 4.94065645841246544E-324 to 1.79769313486231570E+308 and negative numbers from -1.79769313486231570E+30 to -4.94065645841246544E-324.
Double variables are typically used when developing scientific applications using Visual Basic and, as such, are generally not used by the average developer.
Integer Variable
The Visual Basic Integer variable type is used to store whole numbers between -2,147,483,648 and 2,147,483,648. The Integer variable cannot be used to store numbers containing decimals.
Object Variable
The Object variable is a catch-all variable type which can be used to store a value of any data type. Because of this flexibility, an Object variable will typically reserve more memory than is needed for most variable types. For this reason, it is more efficient to use the correct variable type for the value you need to store (such as Char for a character or Integer for a whole number) rather than use the Object variable type.
Long Variable
The Visual Basic Long variable type is used to store whole numbers ranging from -9,233,372,036,854,775,808 to 9,233,372,036,854,775,807.
Short Variable
A Short variable in Visual Basic stores a whole number in the range -32,768 to 32,767.
String Variable
The String variable type stores multiple characters that make up words or sentences. String variables are encapsulated in double-quotation marks ("). For example "Hello" is a string, as is "Hello, this is a test".
Visual Basic provides a number of mechanisms for working with and manipulating strings as covered in the Working with Strings in Visual Basic chapter of this book.
Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99 |