Variables and Data Types

Computer programs are processors of data. Therefore, they need of way of representing various types of data -- numbers, alphabetic characters, special characters, and the like -- inside the computer. Also, programs need a way to hold onto -- to store -- these data inside computer memory during processing. This tutorial discusses these two most basic aspects of computer programming, how to represent and store information involved in computer processing.

Recall from the previous tutorial that all Visual Basic statements appearing on a Web page must be enclosed inside <script> tags, and more particularly inside containers such as subprograms, the Page_Load subprogram being a prime example. Although these tags are not shown in the following examples, they are implied, along with the assumption that scripts appear at the top of the Web page unless otherwise indicated.

Declaring Variables

Variables are named storage areas inside computer memory where a program places data during processing. A program sets aside these storage areas by declaring variables, that is, by assigning them a name and indicating what types of data will occupy these areas during processing.

Within a Visual Basic program, variables are declared using the Dim statement whose general format is shown below.

Dim variable As type

where variable is a programmer-supplied name for the storage area and type is one of the data types permissible in Visual Basic.

Naming Variables

Programmer-supplied variable names are also referred to as identifiers, and they must conform to Visual Basic naming conventions. A variable name must

Variable names are not case sensitive, so the name expressed in upper-case characters refers to the same storage area as a name expressed in lower-case characters. However, for programming consistency, and to avoid unnecessary errors, it is best to express a variable as it is originally declared. Thus, valid variable names include X, My_Variable, MyVariable, My_Data_Variable, and other names using simple and meaningful identifiers.

Data Types

When declaring a variable an indication can be given about what type of data will be stored. There are many data types permissible under Visual Basic. The following are common for declaring the usual types of data involved in computer processing.

Variables, then, are declared by assigning a name and a data type. The following are examples of valid variable declarations that can be used to store data of the identified types.

Dim My_Counter As Integer
Dim My_Salary As Decimal
Dim My_Great-Big-Number As Double
Dim My_Name As String
Dim My_Birthday As Date
Dim My_Current_Time As Date
Dim My_Decision As Boolean

Assigning Values to Variables

Variable declarations simply set aside memory storage areas as temporary containers of data -- temporary because they are no longer accessible once the program ends. During processing, of course, these variables are occupied by data values.

Data for processing can come from many different sources. They can come from outside the program -- from external files and databases; they can come from inside the program by explicitly setting a value for a variable or by storing processing results of manipulating other variables. In any case, variables take on values by assigning values to them.

Variables are assigned values with the Visual Basic assignment statement whose general format is shown below.

variable = value

The data value on the right of the equal sign is assigned to (is placed in) the variable identified on the left of the equal sign. Data values normally originate in one of three ways.

Assigning Constants to Variables

A literal data value -- a particular number, string, date, or Boolean value -- can be assigned to the variable. A number is assigned by specifying its value to the right of the equal sign:

My_Integer = 10
My_FloatingPoint = 12.34567
My_Decimal = 123.45

You can visualize the declaration and assignment of a numeric value to a variable by clicking the two radio buttons below in sequence, representing program execution of the two statements.

Dim My_Decimal As Decimal
My_Decimal = 123.45

An string value is assigned to a variable by enclosing its value inside quotation marks:

My_String = "This is a string."
Dim My_String As String
My_String = "This is a string."

A date or time value is assigned to a variable by enclosing a particular date or time inside "#" symbols:

This_Date = #01/01/05#
This_Time = #12:00:00#
My_Birthday = #July 15, 1942#

Notice that you can store any common representation of dates, whether in the format #mm/dd/yy#, #mm/dd/yyyy#, #mm-dd-yy#, or #Month Day, Year#. Visual Basic converts all formats to its standard internal representation.

Dim My_Date As Date
Dim My_Time As Date
My_Date = #01/01/05#
My_Time = #12:00:00 AM#

Incidentally, if there are two or more variables of the same type to be declared, you can declare them all on the same line by separating them with commas:

Dim This_Date, This_Time, My_BirthDay As Date

A Boolean value is assigned to a variable by specifying True or False:

Error_Flag = True
Dim Error_Flag As Boolean
Error_Flag = True

Variables that have been assigned literal values that do not get changed during program execution are called constants. When declaring constants it is common practice to combine the declaration and assignment into a single statement:

Dim My_Decimal As Decimal = 123.45
Dim My_String As String = "This is a string."
Dim My_Date As Date = #01/01/05#
Dim My_Number As Decimal = 123.45
Dim My_String As String = "This is a string."

Assigning Variables to Variables

Variables can be assigned to variables. That is, the data value contained in one storage area can be assigned to a second storage area.

Variable_B = Variable_A

When a variable is assigned to a variable, the value being assigned is copied to the target variable. The end result is that both variables contain the same value.

Dim Variable_A As Integer = 10
Dim Variable_B As Integer = 20
Variable_A = Variable_B

Assigning Expressions to Variables

The most common scenario is that the results of processing are assigned to a variable. Some type of arithmetic, string, or date manipulation operation takes place with the "answer" stored in the variable. Although these types of operations are discussed later, you should be able to understand the principles involved in the following assignment statements.

The_Answer = 2 + 2
The_Result = 3 * (Var_A + Var_B)
Full_Name = First_Name & " " & Middle_Name & " " & Last_Name

Variable The_Answer stores the value 4; variable The_Result stores 3 multiplied times the contents of Var_A plus the contents of Var_B; variable Full_Name stores the results of stringing together the contents of variables First_Name, Middle_Name, and Last_Name, placing literal blank spaces (" ") between them.

Dim The_Answer As Decimal
Dim Var_A As Decimal = 2.5
Dim Var_B As Decimal = 3.5
The_Answer = 5 * (Var_A + Var_B)

The Variant Data Type

If variable types are not explicitly declared -- using As Integer, As Decimal, As String, etc. -- then values assigned to the variable are stored as a Variant data type. Any type of data can be stored in the variable and any type of operation can be performed on it. It is a general-purpose data type that provides flexibility in storing different types of data in a single variable. It can be assigned a numeric value that is immediately replaced by a string value.

Although it does offer some programming flexibility, there are minor inefficiences associated with converting different data types for representation in the same storage area. For this reason it is best always to declare specific data types for specific variables. This practice of using strong typing of variables leads to more efficient programming and reduction in errors from inadvertently storing the wrong kinds of data in the wrong variables.

Type Conversions

Values of one data type can be converted to another data type. This operation is called casting from one type to another. Casting requires calling a conversion function to switch a data type. The resulting cast must be assigned to a variable of the cast type.

variable = Cname(value)

The Cname is one of the Visual Basic casting functions. For example, the following code converts an Integer to a Boolean type.

Dim A_Number As Integer
Dim A_Boolean As Boolean
A_Boolean = CBool(A_Number)

The following Cname functions are available in VB.NET.

Casting can also be done with the following general-purpose conversion function,

variable = CType(value, type)

where value is any literal value, variable, or expression that results in a value, and type is one of the data types.

Dim A_Number As Decimal = 123.45
Dim A_String As String
A_String = CType(A_Number, String)

In this example, variable A_String ends up storing the value "123.45".

Running Scripts

It is fairly easy to set up a Web page to run the scripts described here. In fact, it doesn't even require HTML coding, only a script and a means to view script output. The following example page gives the format.

TestPage.aspx
<script runat="server">

Sub Page_Load

  Dim Number_1 As Integer
  Dim Number_2 As Integer

  Number_1 = 10
  Number_2 = 20
  Number_1 = Number_2
  
  Response.Write(Number_1)

End Sub

</script>

Place statements inside the Page_Load subprogram. In order to view the contents of any variables use the Response.Write() statement to display a named variable on the page. When this page is loaded into the browser, or refreshed, the value of the variable is displayed. More that one variable can be displayed by appending (&) its name inside the parentheses,

Response.Write(Variable_1 & Variable_2)

possible including a blank space to separate the values:

Response.Write(Variable_1 & " " & Variable_2)

Note that the page has the .aspx extension and that it is run from a remote server or from the local PC running XP Professional with Internet Information Services (IIS) installed.