ASP: The Beginning - Working with Variables
(Page 3 of 4 )
So now that all of that is out of the way, let's get our hands dirty with a little code. In this first example I will beat you until you learn the proper way to declare a variable:
<html>
<body>
<%
Dim teacher
teacher=”Mister Kotter”
response.write(“Welcome back “ & teacher)
response.write(“Welcome back, welcome back, welcome back”)
%>
The above code displays the following text in the users browser:
Welcome back Mister Kotter
Welcome back, welcome back, welcome back
The above code creates a variable named, “teacher” and stores the value “Mister Kotter” in it. We then use response.write to write the sentence “Welcome back” to the monitor, and use “&” plus the variable name to append the data within the variable to the sentence.
Arrays
Another way to think of a variable is to think of a file folder that has some information in it. You can take that data out, read it, put it back, or put other data into it; the information in it varies. Now when you store those file folders, you place them inside of a filing cabinet. In ASP (and pretty much every programming language), you store multiple pieces of data in arrays. Here is how you do it:
<html>
<body>
<%
Dim ateam(3),i
ateam(0) = “Hannibal”
ateam(1) = “Murdock”
ateam(2) = “Faceman”
ateam(3) = “B.A. Baracus”
For i = 0 to 3
response.write(ateam(i) & “<br />”)
Next
%>
This program loops through all the values in the array and prints them to the monitor (don't worry if the code looks a little weird to you; we will cover loops soon). The result is:
Hannibal
Murdock
Faceman
B.A. Baracus
You will note that when we declared our variable we placed a “3” in the brackets. These brackets determine the amount of elements that are in the array (each piece of data in an array is an element). As you can see, there are actually four pieces of data, which does not correspond with the number of elements we initially declared. This is because ASP (and all languages really) start the first element position at 0, and not at 1.
It is possible to create more dynamic arrays, where the number of elements in the array change throughout the course of the program. To do this, you simply leave off the number of elements in the array, like so:
<html>
<body>
<%
Dim ateam()
ReDim ateam(1)
ateam(0) = “Hannibal”
ateam(1) = “Murdock”
ReDim Preserve ateam(3),i
ateam(2) = “Faceman”
ateam(3) = “B.A. Baracus”
For i = 0 to 3
response.write(ateam(i) & “<br />”)
Next
%>
This code creates an array named ateam. It then uses the ReDim keyword to recreate the array to hold two elements and stores some values in it. Next, we use ReDim on the array again, however this time we don't want our previous data to disappear, so we use the Preserve keyword, to keep the data in the array. We then assign two more variables and finally loop through the array, printing out its values. The result:
Hannibal
Murdock
Faceman
B.A. Baracus
Next: Naming Conventions >>
More ASP Code Articles
More By James Payne