Lesson 4 | ASP constants and variables |
Objective | Create and put data into constants and variables. |
ASP Constants and Variables
In the previous lesson, you saw an example that included defining and storing values in ASP variables.
In addition ASP allows for the use of constants, which hold values, such as the name of the current month or the name of your Web site, which do not change with user input.
In addition to using built-in constants, you can define your own constants using the CONST
keyword.
Built-in Constants and user-defined Constants
Empty |
The empty constant is used to indicate an uninitialized variable value. Empty is not the same as Null
Uninitialized [variable value]: A variable that has no value assigned to it. |
Nothing |
The Nothing constant is used to disassociate an object variable from any actual object. Use the Set statement to assign Nothing to an object variable, like this: Set oVariable = Nothing This will be discussed further in a later module. |
Null |
The Null constant is used to indicate that a variable contains no valid data. Null is not the same thing as Empty. |
True |
The True constant has a value always equal to -1 |
False |
The False constant has a value always equal to 0 |
Here is an example you have seen before, but this time the discount percentage is put into a constant:
<HTML>
<TITLE>Constants</TITLE>
<BODY>
<% 'BEGIN ASP SCRIPT
Dim nTotal 'Initialize a numeric variable
Const cnDiscount = .10
'Set this numeric constant to a value
nTotal = 20
'Set this numeric variable 'to a value–
%>
<!–END ASP SCRIPT ––>
Your $<%=nTotal%> total with a 10%
discount is $<%=nTotal – (nTotal * cnDiscount)%>!
</BODY>
</HTML>
The output from the above example is:
Your $20 total with a 10% discount is $18!
Define Constants
It is good programming and scripting practice to define constants in a specific area of the script or program (usually at the beginning) and to use comments to identify that area.
- User-defined Variables: Variables are best described as boxes into which you can put data. In most languages, a variable can hold only one type of data: text, integers, decimals, or dates. In VBScript there are no specific data types, just a single special type of variable, known as a variant, capable of handling any type of data.
- Use Meaningful Names: Because you cannot determine the data type by the type of variable, it is common practice to name the variable in such a way to remind yourself and other programmers of the intended data type:
Here is a code segment illustrating variable type naming:
<HTML>
.
.
<%
'BEGIN ASP SCRIPT
Dim nTotal 'Initialize a numeric variable
Dim cDescription 'Initialize a character variable
Dim iCounter 'Initialize an integer variable
%>
<!–END ASP SCRIPT ––>
Alternatively, the three variables above could be defined in one statement by separating them with commas. We can define more than one type of variable in the same statement:
<HTML>
.
.
<% 'BEGIN ASP SCRIPT
Dim nTotal, cDescription, iCounter
'Initialize a numeric, character and 'integer variable
%>
<!–END ASP SCRIPT ––>
.
.
Even though you cannot determine the type of the variable by the data type, you can determine the type of data currently in a variable or a result by testing the data with built-in
data-type testing functions.
Data-type Testing Functions
tbaIsArray
(expression)
IsDate
(expression)
IsEmpty
(expression)
IsNull
(expression)
IsNumeric
(expression)
IsObject
(expression)
VarType
(expression)
The data being tested (inside the parentheses) can be any
expression[1] or variable. These functions typically return the values True or False, although the function
VarType
can return any of a number of values (shown below).
0 | Empty (uninitialized) variable Uninitialized [variable value]: A variable that has no value assigned to it. |
1 | Null (no valid data) |
2 | Integer |
3 | Long Integer |
4 | Single-precision floating-point number |
5 | Double-precision floating point number |
6 | Currency |
7 | Date |
8 | String |
9 | Automation object |
10 | Error |
11 | Boolean |
12 | Variant (used only with arrays of Variants) |
13 | Non-Automation object |
17 | Byte |
8192 | Array |
XML DOM Element Type Constants (from file ActionPageUtil.asp):
Constants to use as values for the CreateNode method of the XMLDOM.
Constants:
NODE_ELEMENT, NODE_ATTRIBUTE, NODE_TEXT,
NODE_CDATA_SECTION, NODE_ENTITY_REFERENCE, NODE_ENTITY,
NODE_PROCESSING_INSTRUCTION, NODE_COMMENT, NODE_DOCUMENT,
NODE_DOCUMENT_TYPE, NODE_DOCUMENT_FRAGMENT, NODE_NOTATION
When you define a variable in ASP, ASP determines what kind of subtype to assign by reading the data it holds.
If you want to change the subtype of a variable, you need to
convert it.
Of particular use to you will be the Cstr
conversion function, which converts any expression into a string.
What is Data Conversion?
All variables defined in ASP are of the Variant data type.
The Variant can further be classed into subtypes depending on the type of data it contains. A variable with a string assigned to it has a string subtype, while a variable with a whole number assigned to it has an integer subtype.
To change the subtype of a variable, you need to convert it. This allows you to gain more control over how ASP treats the variable.
For example, suppose you have a variable holding the value of pi, and you want to convert its subtype to integer.
Here is how you might do it:
<%
Dim varPi
varPi = "3.142" 'subbtype automatically assigned as a string
varPi = CInt(varPi) 'subtype converted to an integer
Response.write varPi 'returns 3
%>
The variable varPi will return the number 3.
Because the data type was converted to an integer, the decimal was dropped. If the variable is then converted to a double, the number will change to 3.0. Here is a list of the most commonly used data conversion functions.
Function |
Description |
Cbool |
Returns the variable that has been converted into a variant with the subtype Boolean. |
Cbyte |
Returns the variable that has been converted into a variant with the subtype Byte. |
Ccur |
Returns the variable that has been converted into a variant with the subtype Currency. |
Cdate |
Returns the variable that has been converted into a variant with the subtype Date. |
CDbl |
Returns the variable that has been converted into a variant with the subtype Double. |
Cint |
Returns the variable that has been converted into a variant with the subtype Integer. |
CLng |
Returns the variable that has been converted into a variant with the subtype Long. |
CSng |
Returns the variable that has been converted into a variant with the subtype Single. |
CStr |
Returns the variable that has been converted into a variant with the subtype String. |
DateSerial |
Returns the variable that has been converted into a variant with the subtype Date for given year, month, day. |
DateValue |
Returns a value representing a variant of subtype Date. |
Hex |
Returns a string containing the hexadecimal value of a number. |
Oct |
Returns a string containing the octal value of a number. |
Fix |
Returns the integer portion of a number. |
Int |
Also returns the integer portion of a number. |
Arrays
VBScript also allows for the use of arrays. Note the use of the variable names in this code segment to identify them as a(rrays) of i(nteger) and c(haracter) data:
<HTML>
.
.
<% 'BEGIN ASP SCRIPT
Dim aiData(2,3)
'Initialize a 2x3 array of integer values
Dim acCharData(5,4)
'Initialize a 5x4 array of character values
%>
<!–END ASP SCRIPT ––>
.
.
VBScript Arrays
VBScript can create an array of up to 60 dimensions. However, using more than three dimensions makes your ASP script hard to visualize and harder for another programmer to understand. The next lesson discusses how to create formulas and test variables and constants.
ASP Constants - Exercise
[1] Expression: A combination of variables, constants, and operators that produces a value.