Tuesday, April 16, 2024

ASP Primer: Some Basic VBScript

Use these bookmarks
to jump around the tutorial:

[The Dim Statement]

[The If .. Then Statement]

[The Case Statement]

[What’s Next?]

In order to get started
really making good use of ASP we will need to spend some time going over the
most common programming components. If you are already quite adept at either
VBScript or JavaScript, you can certainly skip past the next few installments of this series. If you are new to programming, however, stick with us and we’ll walk you through everything that you need to know to get started.

The
DIM Statement

So, what the heck is a DIM…and what DIM-wit came up with the name? Well, DIM actually stands for dimension and is a hold over from the original BASIC language. 

What DIM does is let the
computer know what variables you are going to use. The computer needs to know
what variables you will use in order to reserve some space for them.

DIM also has one other
very important function.  When DIM is used in cooperation with OPTION
EXPLICIT, it forces you to declare all variable that are used in your ASP page.
While that may not sound like anything important, forcing you to declare all
your variables keeps you from making typos, which makes debugging your code so
much easier.

Here’s how DIM works:

  DIM strName,
intNumber, decNumber

Pretty simple, eh? 
Now we have three variables declared: strName, intNumber, and decNumber. 
The variables are now ready for use in you ASP page. 

Now here are some
details you need to be aware of with regard to how VBScipt handles its
variables. Unlike most other programming languages which require you to declare
the variable name and type, VBScript does not require the variable type.

A variable type,
logically enough, refers to the type of data the variable will hold.  In
our example above, we declared a variable called strName.  strName is our
variable we will use to hold string data, in this case, a person’s name.

If you are new to
programming, you may want to take a look at the table below that outlines some
of the most basic data types. Most programming languages have many more data
types than are listed below. For the purposes of learning basic programming,
though, we will mainly concentrate on the data types listed.

String Holds a string of alphanumeric characters (numbers, letters, and
symbols). An example of this would be someone’s name, address, or telephone
number.
Integer Holds whole numbers. (e.g. 1, 2, 3, 4, etc.)
Decimal Holds numbers with a decimal point (e.g. 16.42, 6.92456, etc.)

So, why even talk about
data types if VBScript doesn’t require me to declare them?

Well, knowing the
different data types will allow you to better understand how you can manipulate
the data. In VBScript there are many different built-in functions that will do a
lot of your programming work for you. In order to use those functions you will
need to know what kind of data is in the variable that you are performing the
function on. For example, doing a square root function on our variable strName
won’t get you a useful result. It’s kind of hard to take the square root of
someone’s name.

I’m sure that you
noticed that each of the variables listed above has a prefix attached to it.
That is my own little system for keeping track of what the data type is for each
variable. I have found it to be very handy when debugging or adding to my code.
The prefix quickly tells me that strName holds a string, intNumber holds and
integer and decNumber holds a decimal. There is no set rule to this technique
and you can use whatever naming method you like, however, making it something
intuitive is always a good idea since you may not always be the person changing
or updating the code.

Extra Hint:

Choose
variable names that make sense. This will help you and anyone else that
may read through your code to quickly and easily know what it is you are
storing in the variable. In our example, strName tells you that a person’s
name will be stored in this variable. The variable names intNumber and
decNumber, however, are far less informative and should be more
descriptive.

back to
top

The
IF .. THEN Statement

And now on to bigger and
better things, the IF .. THEN statement. This will probably be one of the
statements that you use the most in VBScript.

The IF .. THEN statement
is what is known as a conditional statement. In other words, when some condition
is met (or not met for that matter) then something happens. Boiled down to its
simplest form, that is programming in a nutshell. It is simply a series of
events and responses to those events.

Let’s take a look at how
the IF .. THEN works:

  IF strName =
"N/A" THEN

     
strName = "Not Available"

  END IF

In this case we are
checking our string variable strName to see if it is set to "N/A". If it is set
to "N/A" then we change it to say "Not Available" instead. You’ll also notice
that there is an END IF statement. That END IF tells the computer that you are
done with the IF .. THEN statement and it can continue executing any other code.
The computer knows to only execute the code between the THEN and the END IF if
the condition you set is met.

So, what kind of
comparisons can I do?

There are quite a few
comparisons that you can make. Here they are along with the data types that they
apply to:

= Equal to. You can use this to compare any data type.
< Less than. You can use this to compare any numeric data type.
(You can also use it for string variable types as well but you will probably
find that you never need to except when comparing dates.)
<= Less than or equal to. You can use this to compare any numeric
data type. (You can also use it for string variable types as well but you
will probably find that you never need to except when comparing dates.)
> Greater than. You can use this to compare any numeric data type.
(You can also use it for string variable types as well but you will probably
find that you never need to except when comparing dates.)
>= Greater than or equal to. You can use this to compare any numeric
data type. (You can also use it for string variable types as well but you
will probably find that you never need to except when comparing dates.)
<> Not equal to. You can use this to compare any data type.

When using the
comparison operators above always remember to put string comparisons in quotes
like in the example above. Numeric comparisons, like you will see below, do not
use quotes.

So, what if I wanted to
make more than one comparison? Do I have to list one IF .. THEN after another?

That’s where ELSEIF
comes in. ELSEIF allows you to string several comparisons together. Here is an
example:

  IF intScore > 90
THEN

     
strGrade = "A"

  ELSEIF intScore >
80 THEN

     
strGrade = "B"

  ELSEIF intScore >
70 THEN

     
strGrade = "C"

  ELSEIF intScore >
60 THEN

     
strGrade = "D"

  END IF

With the short section
of code above you can easily see that we are assigning a grade based on
someone’s score. You can also see that ELSEIF works exactly like IF in that it
makes a comparison the exact same way with comparison operators.

There is one very key
difference to keep in mind, though. This type of IF statement is executed in
sequence. In other words, if the first condition isn’t met it moves on to the
second condition. If the second condition isn’t met then it will move on to the
third condition. If the third condition is met it will execute the
appropriate code which, in our case, is strGrade = "C". Once the code has been
executed the computer considers itself done and doesn’t even bother looking at
the fourth condition. In our case that’s exactly what we want it to do since we
only need to assign one grade based on the score.

But what if I wanted to
make 4 comparisons and have each one be considered regardless of the result of
the comparison?

In that case you will
have to create 4 separate IF .. THEN statements in order to be assured that each
condition will be checked.

Now there is one more
piece of the puzzle. If you noticed above there was no "F" grade. That’s where
ELSE comes in. ELSE is a sort of catch-all. It simply means that if none of the
conditions before it are met then it will execute its code. Here’s our revised
IF .. THEN with ELSE included:

  IF intScore > 90
THEN

     
strGrade = "A"

  ELSEIF intScore >
80 THEN

     
strGrade = "B"

  ELSEIF intScore >
70 THEN

     
strGrade = "C"

  ELSEIF intScore >
60 THEN

     
strGrade = "D"

  ELSE

     
strGrade = "F"

  END IF

Now if the student
didn’t score at least 60 then they will receive and "F".

Make a Note …

If you
noticed, END IF is two words but ELSEIF is only one word. This is a little
detail that trips up both new and seasoned programmers. You’ll probably
find yourself getting them switched more than once.

back to
top


The CASE Statement

Now that you have an
idea of how conditional statements work we’ll throw out another one to you, the
CASE statement. The CASE statement is very much akin to the IF .. THEN.

For our example of the
CASE statement we’re going to give you a practical example. Whenever possible
we’ll try to give at least one practical example with each segment of our primer
series. Here’s your practical example for this segment:

Use the code below to
create an ASP page called nav.asp:

<% OPTION EXPLICIT %>

<% ‘ This example
creates a form with a drop-down %>

<% ‘ menu. The user
selects a page from the list and %>

<% ‘ clicks GO! to move
from page to page. Each time %>

<% ‘ the user clicks GO!
they are sent to a page named %>

<% ‘ nav.asp where their
selection is evaluated by a %>

<% ‘ CASE statement. The
CASE statement then %>

<% ‘ redirects the user
to the appropriate page. %>

<% SELECT CASE
Request.Form("Category") %>

<%   CASE
"Home" %>

<%    
Response.Redirect "index.html" %>

<%   CASE
"About Me" %>

<%    
Response.Redirect "about_me.html" %>

<%   CASE
"About HTML" %>

<%    
Response.Redirect "about_html.html" %>

<%   CASE
"About ASP" %>

<%    
Response.Redirect "about_asp.html" %>

<% END SELECT %>

Create a new HTML page
with the code below or just cut and paste the form portion of the page into an
existing HTML or ASP page:

  <HTML>

    <HEAD>

      <TITLE>My Navigation Page</TITLE>

    </HEAD>

    <BODY>

     
<FORM METHOD="post" ACTION="my_page.asp">

     
<P>

       
<FONT COLOR="#000080">

         
Please select a page:</FONT>

       
<SELECT NAME="Category" >
          <OPTION
SELECTED>Home</OPTION>
          <OPTION>About Me</OPTION>
          <OPTION>About
HTML</OPTION>
          <OPTION>About
ASP</OPTION>
        </SELECT>

       
<INPUT TYPE="submit" VALUE="GO!"

         
NAME="Go">

       
</P>
      </FORM>

   
</BODY>

  </HTML>

Now here’s how it all
works. Once a user selects a page in the drop-down menu and clicks the GO!
button they are redirected to a page called nav.asp. That page evaluates the
user’s request in a CASE statement and then forwards them on to the page that
they requested. This all happens so quickly that the user probably will never
realize they were sent to an intermediate page before arriving at the page that
they requested.

Let’s break down that
CASE statement now. The first thing you see is SELECT CASE followed by what is
to be evaluated. In this case, we are evaluating the information that is sent
from the form on your HTML page. (Don’t worry about the Request.Form or the
Response.Redirect for now. We will get to those concepts later on in this
series.)

Next we list out each
possible match using CASE. In our example above we have 4 items in the drop-down
menu. Therefore, we need to evaluate for those 4 items in our CASE statement.
Beside each CASE is a possible match: "Home", "About Me", etc. Then below each
CASE we have the instructions for what we want the computer to do. In this case
we are telling our server to reroute the person to the page that they selected.

If the example above
doesn’t make sense to you consider this example using strGrade instead:

<% SELECT CASE strGrade
%>

<%   CASE "A"
%>

<P>Congratulations! You
got an A!!!</P>

<%   CASE "B"
%>

<P>Good Job! You got an
B!</P>

<%   CASE "C"
%>

<P>Not Bad. You got a
C.</P>

<%   CASE "D"
%>

<P>Well, Duh. You could
do better than a D.</P>

<% END SELECT %>

The CASE statement above
mixes VBScript and HTML on an ASP page. Depending on what grade the student
received, the appropriate message is displayed on the page using standard HTML.

If the IF .. THEN and
the CASE statement both get you the same result then why use CASE at all?

Well, CASE has a couple
of advantages. It is a much more concise code which means less typing. It also
is usually more intuitive than a very long series of IF .. THEN statements.

Feel free to plug in and
adapt the code above to your pages. Depending on the circumstance, the example
above can be a very handy solution for navigation problems.

back to
top


What’s Next?

Now that you have
mastered declaring variables and some of the conditional statements we will move
on to more VBScript concepts.

In the next part of this series
we will:

  • Learn about FOR ..
    NEXT loops

  • Learn about DO loops

  • Learn about arrays

back to
top

<< Previous | Next >>

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured