When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Interactive Art Director
Aquent
US-TX-Houston

Justtechjobs.com Post A Job | Post A Resume

Variables in ASP


Declaring variables in ASP is simple, especially since all variables are of type Variant. You don't have to declare if your variable is an integer, string, or object. You just declare it, 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 Variable, 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)
%>

FAQ Table of Contents   The Response Object Functions in ASP 


Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers