ASP Variable
ASP variables are declared using VBScript declaration type.
Assumming VbScript is used, this is how you declare variable:
DIM varaibleName or Const variableName.
If you want reject undeclared variables, use
<% Option Explicit %> at the beginning of your page.
You would often see these two lines:
<%@ Language="Vbscript" %> <% Option Explicit %>
at the beginning of ASP pages. First line sets the language
and the second line watches undeclared variables. VBScript and
JavaScript variables are variant type variable, which means they can
take any type of values. Any variable name must start with letter or
underscore. Watch this program to see how variables are used:
<%
Dim name, email, age name="John M"
email="you@you.com" age=356
response.write("Your Name: " & name & "<br>")
response.write("Your Email: " & email & "<br">)
response.Write("Your age: " & age);
%>
The following is the result from the above example:
Your Name: John M
Your Email: you@you.com
Your Age: 365
ASP Arrays
An array is an indexed list of things called elements or group of related variables.
For example, say that we want declare variables for list of cars like this;
Dim car1, car2, car2, car3,....
Here is how you would declare list of cars using array:
Dim cars(3); We simply declared array that takes
4 items. To assign values to this array, do these:
cars(0)="Jeep Grand Cherokee"
cars(1)="Jeep Wrangler"
cars(2)="Jeep Liberty"
cars(3)="Jeep Cherokee Briarwood"
Use this statement to write an item in the array:
response.write(cars(3)) This will write the
4th car on the list.
Here is the example we did in code
| Example | Explanation |
<%
dim cars(3)
cars(0)="Jeep Grand Cherokee"
cars(1)="Jeep Wrangler"
cars(2)="Jeep Liberty"
cars(3)="Jeep Cherokee Briarwood"
response.write(cars(0)&", ")
response.write(cars(1)&", ")
response.write(cars(2)&", ")
response.write(cars(3)) %> |
This is simply illustration, NOT good example.
If you writing items from an array, you would probably write
all the items at one time using loops instead of listing response.write statements.
|
To write the array using for loop statement, do this.
| Example | Explanation |
<%
dim cars(3)
dim x
cars(0)="Jeep Grand Cherokee"
cars(1)="Jeep Wrangler"
cars(2)="Jeep Liberty"
cars(3)="Jeep Cherokee Briarwood"
for x= 0 to 3
response.write(cars(x)&" ")
next%> |
The variable x which starts from 0 to 3 acts as an index for the
array.
|
Two dimensional Arrays
You can define two or more dimensional arrays by puting numbers withing the parenthesis.
Here is how you would define two dimensional array Dim cars(3,3).
The reason you may use this type of array is when you have records of each item. For example,
car, year, price,... and you want display one record at a time.
The following is an example of two dimensional array:
| Example | Explanation |
<%
dim car1(3,2)
dim x,i
car1(0,0)="Jeep Grand Cherokee"
car1(0,2)=2002
car1(1,1)="Jeep Wrangler"
car1(1,2)=2002
car1(2,1)="Jeep Liberty"
car1(2,2)=2003
car1(3,1)="Jeep Cherokee Briarwood"
car1(3,2)=2003
for x=0 to 3
for i =0 to 2
response.write(car1(e,&a))
next
response.write(" "&" ")
next> |
This array has three cars and each car has two fields or columns.
It's like a table, row 0 has record of one car, row 1 has record of another car and so on
We also define nested for loops. First one reads the row and second one reads the column. |
|