View contents of this chapter

Variables: Gettign Started

Variables are containers that store data in our programs. The value of a variable can change because it is nothing more than a storage area.

Play Sound A variable is a temporary named storage area inside your program's memory that hold data.

In programming, variable names are usually descriptive of the contents they hold. You are responsible for naming all the variables in your code.Two different variables cannot have the same name within the same procedure.

The Dim statement declares variables by assigning them a name and data type. You can use a variable without declaring it but that produces errors at times.

Play Sound If you don't declare a data type, Visual Basic assumes it of variant type.

Here is the format of the Dim statement:

Dim varName as DataType

Where: varName is the name of the variable
DataType is one of the data types listed in the previous sections.

When naming variables, you must:

  • Begin all variables with an alphabetic letter.
  • Use letters or numbers in the name.
  • Keep the name from 1 to 255 characters in length.
  • Use a limited set of special characters in the name.

Here are some possible variable declarations using Dim:

Dim intTotal as Integer
Dim curAmount as Currency
Dim strName as String
Dim blnIsChecked as Boolean

Play Sound Use names that are meaningful instead of something that is ambigious.

After you declare a variable, you can store data in the variable. Using the assignment statement is the easiest way to store data. Let's have a look at the following statement:

strName = "VB"

The above statement assigns the value "VB" to the strName variable.



© Universal Teacher Publications        INDEX Previous Screen Next Screen