|
Understanding the Different Types of Variable/Terminology
When you start learning any programming language, it is essential that you
familiarize yourself with the terminology of that programming
language. The table1 below lists the most important VB terms that
you'll come across throughout this tutorial:
| Term |
Description |
| Design time |
When the
application is still being developed in the Visual Basic environment. |
| Run time |
When the application is
executed. At run time, the programmer interacts with the application as the user would. |
| Forms |
Windows that can be customized to serve as the interface for an application or as dialog boxes used to gather information from the user. |
| Controls |
Graphic representations of objects, such as buttons, list boxes, and edit boxes, that users manipulate to provide information to the application. |
| Objects |
A general term used to describe all the forms and controls that make up a program. |
| Properties |
The characteristics of an object such as size, caption, or color. |
| Methods |
The actions that an object can perform or that can be performed on the object. |
| Events |
Actions recognized by a form or control. Events occur as the user, operating system, or application interacts with the objects of a program. |
| Event-driven programming |
This is the way
VB programs execute. Unlike older sequential programming
languages (also known as procedural programming) where the code is
executed line by line, Visual Basic executes code in response to events invoked by the user, operating system, or application. |
So now that we have
that out of the way, it is time for yet another table. This time,
you're familiarizing yourself with the different types of variables that
can be declared in VB. Variables are simply containers that hold
numbers or characters. The name 'variable' means that the values in
these "containers" can be changed at any time. Variables
make it extremely easy for you to pass information from one control to
another in your program. For example, if you want users to type in a
web address in one form and then transfer that address to another form
that contains a browser you could use variables: Dim strURL=txtURL.text.
The variables that will be covered in this tutorial are marked with
a '*':
| Variable |
Description |
Decimal
Precision |
Storage
size |
| Integer* |
Stores
only whole numbers from -32,768 to
32,767. |
0 |
2
bytes |
| Long
Integer |
Stores
only whole numbers from
-2,147,483,648 to 2,147,483,647. |
0 |
4
bytes |
| Single |
Stores
numbers from -3.402823E38 to -1.401298E-45
for negative values and from 1.401298E-45 to 3.402823E38 for positive values. |
7 |
4
bytes |
| Double* |
Stores
numbers from -1.79769313486231E308 to -4.94065645841247E-324 for negative values and from
1.79769313486231E308 to 4.94065645841247E-324
for positive values. |
15 |
8
bytes |
| String* |
Text
or combination of text and numbers, as well as numbers that don't
require calculations. |
n/a |
up to
255 characters |
| Date |
Date
values for the years 100 through 9999. |
n/a |
8
bytes |
| Variant |
When
you don't specify the data type for a variable, VB makes it a
variant. Variants can store any type of value. |
- |
- |
| Currency |
Currency
values and numeric data used in mathematical calculations
involving data with one to four decimal places. Accurate to 15
digits on the left of the decimal point and 4 digits on the right. |
4 |
8
bytes |
| Boolean |
A
variable that is used for operations that include Yes/No,
True/False, On/Off Values. |
n/a |
1 bit |
| Byte |
Stores
whole numbers from 0 to 255. |
0 |
1
byte |
In this course, we'll
only be using Integers, Doubles and Strings, however, I have the others
here just so you are aware of all the options open to you. Now it's
time for you to see how these variables are declared in VB.
Declaring a variable means that you are creating a variable and defining
its data type. Visual Basic automatically initializes numeric variables to 0, and text strings or variants to empty ("").
After declaring your variable, you can use it freely in your current form.

To
declare a variable in VB, use the Dim statement: Dim variablename [As type]
For example,
Dim
MyName as String When
using the Dim statement, variables are only visible in their
current form/module. In order to allow a variable to function
throughout the project, you are going to have to change the variable's
"scope". In other words, replace Dim with another statement:
| Statement |
Description |
| Dim or Private |
A variable that
is visible to all procedures within the module or form where it is
declared. This is done in the General Declarations section (The
very top of the form/module) using the Dim or Private
keywords. If a variable is declared within a
procedure, then it becomes a local variable and is only visible to
anything that is inside that procedure. Example: Private
MyNumber as Integer |
| Public |
A variable
declared at the module or form level that is visible to all
procedures within the project. Public variables must be declared in
the General Declarations section. Example: Public MyNumbr as
Integer makes
MyNumber visible to all forms/modules within the
project. |
Rules for Naming
Variables in VB:
Must begin with an alphabetic character
May not contain spaces
Cannot contain a period or any of these characters: (%, &, !, #, @, or
$).
{Go Back To VB Lessons Page}
________________________________________________
1. Extracted
partly from Microsoft VB Student Workbook
|