Data types

Data types are essential part of any programming language primarily because they allow error checking and determine the size of memory allocation, what kind of information can be stored, and what operations can be performed to a variable. However, in VBScript we do not need to worry about specifying the data type of a variable because our use of the variable and its context will determine what operations can be performed on the variable.

What is data type?

To make programming languages more efficient, data is categorized into most common types: integer, float, double, string, Boolean, date, and currency. Except for currency, all of these data types will be discussed in this chapter in the order they are listed. An integer type indicates that our data type is a whole number such as -2, -1, 0, 1, and so on. If we wanted to find out how many classes a student is registered for this semester, we can be certain that the response would be a whole number: 0 or greater. However, if we wanted to find out the name of the student, we should expect a response that contains letters of the alphabet.

Unlike C/C++, VBScript allows any type of data assignment to a variable. For example, a variable called NumberOfClasses could represent the number of classes a student is registered for. In addition, this same variable in the same program can also represent and store the name of the student. In C/C++, however, this is not possible because a data type number must store numerical values only, not other data types.

Integers

An integer is a whole number, without a fractional portion. Examples include number of automobiles a student owns, number of hours in a day, number of days in a year, etc. Using the correct data types in any program is a very important step because then the programmer can decide what operations can be performed on the data. If for example, an integer is expected from the user, then, the programmer has to make sure that only mathematical operation are performed. In other words, only mathematical operation can be performed on numerical values.

Floats

A float is a number with fractional portion such as -1.3, -.45, 4.56, 10.0000, etc. Examples include 1 divided by 3, amount due at a check-out, etc. Maintaining floats' precision requires more memory than integers which do not have fractional parts. As a developer of scripts for ASP, you need not worry how this is handled because VBScript automatically determines the data type from the context. VBScript will automatically allocate sufficient memory to perform the operations on any data type. Consider a simple example, if you were doing integer division: 10 divided by 2. The answer 5 will be stored as an integer. However, dividing 1 by 3 has infinite "3"'s to the right of the decimal point; consequently, the result won't be stored as an integer.

Double

A double is similar to floats except the precision is twice as much as of a floating-point number. An interesting example is temperature, which has many trailing digits but is rounded-off to an integer for simplicity. Thus doubles have more precision and require more memory.

String

A string type consists of letters, numbers, or symbols. "Active Server Pages", "5mph", and "15" are all example of strings. It is very important to note that the strings are included inside of double quotation marks.

Boolean

Boolean types are used to indicate any one of two states, for example, TRUE or FALSE, 0 or 1, ON or OFF, etc. A Boolean type is used to make decisions depending on the value of the variable used to represent Boolean type.

Date

A date type can hold time or date. It is a great feature of VBScript. The date type can be used to represent strings and/or integers.