Back to Basics: VBScript for ASP - Part 2
March 5, 2001
|
In part two of this "back to basics" approach with VBScript for
ASP, we will cover some more advanced techniques to make you and
your pages work better in the long-run!
|
Introduction
Last month
marked the start of our journey into learning basic
server-side VBScript for use in
ASP pages.
We learned about variables and types, conditional testing and
some looping. This month we will take a look at some slightly
higher-level techniques that can be very useful for making your
ASP pages more functional and well-polished. We begin with a
discussion of Arrays.
Array Basics
Arrays are a very useful "tool" at your disposal as a developer.
Very simply put, arrays give you the power to store multiple
values in just one variable. An even better way to think of an
array is to return to your algebra days in middle school or high
school. Remember matrices? Basically they are a table of numbers
or variables upon which you could perform various operations.
Arrays are just that, matrices in your computer.
Arrays in VBScript are easy to implement and to use, you just
need to be clear on the syntax and how they work conceptually.
Let's begin by taking a look at setting one up and using it.
Dim myArray(10)
Dim myString
myArray(0) = "Power of"
myArray(1) = "Cheese"
myString = "The " + myArray(0) + " " + myArray(1)
The code snippet above does a couple of simple tasks. First, it
initializes the array, called myArray using a
Dim statement. Notice how myArray is
followed by (). Those two parentheses with a number
inside determine that the variable is of sub-type array and how
many values it holds. This particular array holds ten values (of
which we use only 2).
Okay here comes the hard part. myArray holds ten
values, or elements, but in VBScript arrays are zero-based
- that means they start counting from zero instead of one.
Therefore, the first element of every VBScript array is zero!
Having an array with ten elements means myArray(9)
is the last element not myArray(10). Do not
get that confused or you will be pulling your hair out for hours
on end trying to figure out why your code is generating so many
hiccups.
As you can see, to set an element of an array equal to something
like a string, number or even another array, it is as simple as
using the equals operator. Reference the appropriate element
within the parentheses on the left, provide some value on the
right and you are good to go. Above, I have set element zero to
"Power of" and element one to "Cheese." Then, I combine all
these values in myString to form the phrase: "The
Power of Cheese."
Working with Arrays
There are also other ways to use an array, and even other ways of
declaring one. Let's take the following example and work through
it to its conclusion using arrays. Assume that you need to
collect a number of survey responses but do not know beforehand
how many will come back in. We will also assume that each survey
only asks one question and the answer is either "True" or
"False." Without worrying about processing the results after we
have stored them, how can we do this using VBScript and arrays?
First, we need to determine what variables we will need and then
declare them. I can think of two to start.
<%
Dim surveyResults()
'Notice how I left the parentheses blank
'since we do not know how many there are yet,
Dim numResults
%>
So far we have declared two variables. One is an array with no
length, called surveyResults. The other is a
variable of type variant called numResults. So far
so good. To continue, we must add another assumption to our
example, that is, that the administrator of these surveys is
prompted for the number of results in the survey. I do not want
to get into that here, so when we set numResults to
some value, pretend it was first obtained from the user.
<%
Dim surveyResults()
Dim numResults
'pretend the user is prompted for
'the following line - perhaps via
'an HTML form.
numResults = 12
ReDim Preserve surveyResults(numResults)
%>
Okay, so there are twelve survey results, the next step would be
to change the length of the array (or the number of elements in
it) to twelve. This step is done using the ReDim
statement above. ReDim works just like
Dim, except it handles dynamic arrays, or arrays
that change their length during the life of the program.
ReDim combined with Preserve ensures
that all the information within our array is saved during
resizing and is available to us throughout the program.
The next step involves looping through the elements of the array,
and I will take that opportunity to introduce a new loop to you,
the While loop!
The While Loop - Page 2
|