ASP   «Prev  Next»

Lesson 4ASP constants and variables
ObjectiveCreate 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.

Built-in constants and user-defined constants

In addition to using built-in constants, you can define your own constants using the CONST keyword.
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.
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.

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

Click the Exercise link below to run and modify an ASP script inside an HTML page.
ASP Constants - Exercise