To read the article online, visit http://www.4GuysFromRolla.com/webtech/faq/Beginner/faq4.shtml

Variables in ASP


Declaring variables in ASP is simple, especially since all variables are of type Variant. You don't have to define the variable's type (such as if it's an integer, string, etc.). Rather, you just declare the variable using the Dim keyword and it has the potential to be anything. Here is some code creating and assigning values to a few values:

<%@ LANGUAGE="VBSCRIPT" %>
<%
    'Declare a variable. A single tick denotes
    'a comment in VBScript
    Dim MyName

    'To declare a variable, you just put the word
    'Dim in front of the variable name.
    'Let's assign something to MyName
    MyName = "Scott Mitchell"

    'Let's create some more variables
    Dim Age, Pi
    Age = 20
    Pi = 3.14159

    'Since all variables are variants, we can
    'Reassign values. (Bad coding, though!)
    MyName = 45
    Pi = Age
    Age = "Yellow Schoolbus!"
%>

If you wanted to print out the variables, you could simply do a Response.Write. Here is an example:

<%@ LANGUAGE="VBSCRIPT" %>
<%
    'Declare a variable. A single tick denotes
    'a comment in VBScript
    Dim MyName

   MyName = "Scott Mitchell"

    'Print out MyName
    Response.Write(MyName)

    'To concatonate strings in VBScript, use the
    'amperstand
    Response.Write("My name is " & MyName)
%>

Since variables are Variant, you may be wondering how you can tell what, exactly, is in them. There are some built-in functions which help you in this task.

<%@ LANGUAGE="VBSCRIPT" %>
<%
    'Declare the variable MyName.
    Dim MyName

    MyName = "Scott Mitchell"

    'Will print out false
    Response.Write(IsNumeric(MyName))

    'Will print out false
    Response.Write(IsDate(MyName))
%>

You can explicitly cast the type of a variable when you assign to it. It is good programming technique to do that! That is because if you have a variable SomeString with the value "5" in it, and you want to assign it to a variable SomeInt, you don't want SomeInt to be a string but an integer. So you could type: SomeInt = CInt(SomeString).

Here is an example:

<%@ LANGUAGE="VBSCRIPT" %>
<%
    'Declare the variable MyName.
    Dim MyName

    'Explicitly make Myname a string
    MyName = CStr("Scott Mitchell")

    Dim Age, Pi

    'Explicitly make MyAge an integer
    Age = CInt(20)

    'Explicitly make Pi a double (a floating point
    'value with double preceision)
    Pi = CDbl(3.14159)
%>


Article Information
Article Title: Variables in ASP
Article Author: Scott Mitchell
Article URL: http://www.4GuysFromRolla.com/webtech/faq/Beginner/faq4.shtml


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers