Active Server Pages/Variable Types
In this section you will learn about variables in Active Server Pages. This includes naming conventions, how they are declared, different primitive types and naming conventions. After studying this section, you should have a firm grasp of the types of variables available to you and how to work with different types in a web application.
All variables in ASP are loosely typed. This means that you don't need to declare variables with a type. It also means that you can assign the value of any variable to any other variable. There are a few exceptions to this rule as we will see.
Basically, every variable in ASP has the major type of Variant. It is "variant" meaning that the type of value it holds can vary. Basically, the ASP interpreter will handle all conversion between types automatically when you group more than one variable or type together in a single expression.
There are three different scopes for variables used in ASP. The first is page scope meaning that the variable is available for the entire duration. These are typically declared at the top of the page. The second type of variables are procedure scoped variables which are declared inside a procedure or function declaration. The third type of variables are scoped with a class definition which is used for object-oriented programming.
<% Dim sStr1 ' page-scoped variable .... Procedure DoSomething Dim mStr2 ' procedure-scoped variable End Procedure Function DoSomething Dim mStr2 ' function-scoped variable End Function Class clsTest Private msStr4 ' class-scoped variable Public msStr5 ' class-scoped variable End Class %>
Page-scoped variables will be visible to all code included on your web page including procedures and functions. You can even access page-scoped variables within a class although good object-oriented design would strongly discourage this practice.
Procedure-scoped variables will only be visible within the procedure in which they are defined. It is perfectly acceptable to use the same variable name for a page-scoped variable and a procedure-scoped variable. Keep in mind, that when you do this, you will hide the page-scoped variable and will have no way to access it within the procedure.
Class-scoped variables can only be accessed through an instance of the class. There is no notion of static variables or static methods in the simplified ASP scripting language. You will need to use the ObjectName.VariableName syntax in order to access member variables directly. Only variable declared Public can be accessed in this way. Private class variables can only be accessed by code defined within the class. More information about using classes in Active Server Pages will be discussed later.
Valid Variable Names
To be valid, an ASP variable name must follow the following rules:
- Must begin with an alphabetic character
- All other characters must be alphanumeric or underscore ("_")
- Name must not exceed 255 characters in length
- Name must be unique within the scope it is defined
- Name must not be the same as a reserved word of built in functions, procedures, and objects.
Variable names, just like statements are case-insensitive. This means that if you declare a variable with the name "myVar", you can access it using "MYVAR" or "myvar" or even "MyVaR".
One way to avoid using reserved words and make it easier to identify what type of variables you are using is to use a naming convention. Usually the first letter or few letters of the variable names what information it holds.
- nCount n for integer or number, holds a value that is used to count something, like for control loops.
- sName s for string, holds a value that represents a name.
- bIsActive b for boolean, holds true or false values, in this case true if something is active, false if it is not.
Following a naming convention can help avoid messy code, and leads to good programming habits. It also allows somebody else to work with your code and help debug it or modify it. Along with a naming convention, use comments as well to document what the variable is to be used for in the ASP page.
Dim nCount 'Used to count values in control loops.
Dim sName 'Used to store the user's name that they enter in a web form.
Dim bIsActive 'Used to test if the user's account is active.
You will learn more on naming conventions later on in this document.
To declare a variable, you use the Dim statement for page and procedure-scoped variables. After the Dim statement, you can put a comma-separated list of variable names. If you prefer, you can just put one variable on each line. This allows you to add comments about each variable you declare.
<% Dim sAction ' form action Dim sQuery ' database query Dim I, J, K %>
You should place your variables in a consistent location on the page or within your procedures. Typically, most people place the declarations at the very top (beginning) of the page or the top of the procedure. Others think it makes more sense to place the variable declarations right above the location where they are first used.
When we talk about primitive types, we are talking about low-level variables types that cannot be broken down into smaller primitive types. Basically, these are the "built-in" variables that Active Server Pages understands.
These are sometimes referred to the "sub-type" of the variable since the major type of the variable is "variant". Each variable may have one of the sub-types shown in the following table. If you have used other programming languages, then these primitive types will be very familiar to you.
|Boolean||True or False||2 Bytes?||true|
|Byte||0 to 255||1 Byte||127|
|String||Any unicode characters||Unlimited||"unicode: ê"|
|Int||-32,768 to 32,767||2 Bytes||147|
|Long||-2,147,483,648 to 2,147,483,647||4 Bytes||3458972|
|Single||1.401298E-45 to 3.402823E38 (pos)
-3.402823E38 to -1.401298E-45 (neg)
|Double||4.94065645841247E-324 to 1.79769313486232E308 (pos)
-1.79769313486232E308 to -4.94065645841247E-324 (neg)
|Currency||-922,337,203,685,477.5808 and 922,337,203,685,477.5807||8 Bytes||132412.34|
|Datetime||01/01/100 to 12/31/9999||8 Bytes||'09/23/2004 12:34 AM'|
|Object||An object reference||8 Bytes?||Server.CreateObject("...")|
In addition to the values shown above, the following special values may also be assigned to Active Server Page variables:
The empty value is the default value for newly declared variables (never assigned any value.) If tested in a boolean context, it is equivalent to "false" or tested in a string context it is equivalent to the empty string ("")
Conversion and Verification
You don't need to concern yourself much with setting the type of the variable. This is done pretty much automatically for you. In the event that you want to set the type that is assigned to a variable, you can use the ASP conversion function that corresponds to each type.
Also, there are verification functions available to make sure a variable is compatible with the specified type. This doesn't mean that the variable has that sub-type. Rather, it means that the variable can successfully be converted to the type.
Literal values are values you insert into your ASP code and use in expressions. By itself, a literal value has no use, but when used in a statement such as an assignment or output statement
<% ' examples of literal values Dim sString, nInt, fFloat, dDate, bBool sString = "Hello There" nInt = 1234 fFloat = -25.324 dDate = DateSerial(2004, 10, 28) bBool = True %>
You will notice that there is no way to specify a date literal in ASP. Therefore, you need to use a function call to build a date value using DateSerial or you can use Now() to get the current date and time.
The value for a literal value is bound by the limits specified in the table for primitive types above. If you attempt to use a value that is outside the acceptable range for any of the types, you will receive an error message indicating this fact and your ASP page will terminate execution.
In some cases, you might want to create "named constants" instead of using literal values all of the time. This is particularly useful if you create a common include file that will be used in multiple scripts on your site. You define constants using the Const keyword like so:
<% Const PI = 3.141592654 Const RECS_PER_PAGE = 25 Const FSO_FORREADING = 1 Const FSO_FORWRITING = 0 %>
It is common practice to use all uppercase variables for your constant definitions although this is not required. It simply makes your code easier to read and maintain.
A naming convention is a standard way of naming all of the variables used on your site. There are various different methods used for naming variables and a whole chapter could be devoted to this subject. One of the most popular is Hungarian notation where the first letter of the variable name indicates the dominant sub-type for the variable.
The Microsoft standard proposed for Visual Basic is to use a 3-letter naming convention for their variables. This is a little more clear for reading and maintaining code. Some would consider this overkill for a light-weight scripting language.