Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
VBS Basics
Introduction
Variables & Arrays
Sub & Function Procedure
If Statements
Case Statements
Loop Statements
Operators
Forms
MsgBox Function
Books @
CA Amazon
UK Amazon
US Amazon
 

VB Script Variables

Variables are used to store information. VB Script Variables are declared like visual basic except, variable type is not specified because VB Script variables are type variant that can store different types of values. For example, The following example are string variable declaration for VB & VBScript;
script variable: dim name,
visual basic variable: dim name as string.
Variable must not exceed 255 characters, cannot contain an embedded period, unique in the scope in which it's declared, and must start with letter. Variable can be declared with dim, public or private.
VBScript allows you to use undeclared variable with out the use of Option Explicit. This kind of use is discouraged because of the risk involved to misuse or misspell a variable. Option Explicit prevents the use of undeclared variable and if used it must appear in the head of the script before any procedures. The following are sample variable to see how to store information into a variable.
dim name
dim CustNno
dim MonthlyFee
name="B. Danyeer"
CustNo="11122"
MonthlyFee=100
We declared three variables; name, CustNo, MonthlyFee and initialized each of them with value. The variable name has the value B. Danyeer. Anything with quotation " " is string and cannot be calculated as a number. MonthlyFee is the only number variable that can be calculated or has number value. If undeclared variable is used, then the [option explicit] will give you an error message. You declare more than one variables in one statement by separating each variable name with a comma.
For example:
Dim firstName, middleName, lastName, Right
You cannot assign value to more than one variable in one statement unless it's an array.

VB Script Arrays

Sometimes you want to assign more than one value to a single variable. This is called an array variable. Here is how an array is declared: dim cars(3) . This simply declares a variable called cars that contain 4 values. Array index or elements start with zero, so it's zero to three that makes up four car- values. An array can be a dynamic array that takes any number of elements by not providing the elements like this: dim cars(), but we deal with fixed-size arrays in this tutorial.

To assign a value into array element, use the name and the index number. Here is an example that assigns values into the array declared above.
cars(0)="Jeep Grand Cherokee"
cars(1)="Jeep Wrangler"
cars(2)="Jeep Liberty"
cars(3)="Jeep Cherokee Briarwood"

Now we know that every element of the array has value. We can retrieve the data one at a time or all at once. For example, I can retrieve Jeep Grand Cherokee by using cars(0) or by re-assigning to it an another variable like this, car1=cars(0).
We can also access to the array data using loop like this:
for x=0 to 3
  msgbox(cars(x))
next

This simply display all the cars on message box one at a time.

Here is a complete example using arrays and variables.
<html>
<body>
<script type="text/vbscript">
dim name
dim age
name=inputbox("Please enter your name:")
age=inputbox("Enter your age:")
winNum=age*2-2+5*3-3+9/2
dim cars(3)
cars(0)="S2000"
cars(1)="Odyssey"
cars(2)="Passport"
cars(3)="Accord"
document.write("Hello " & name)
document.write("Your lucky number to win one of the following Honda cars is: " & winNum &"<br>")
document.write("Cars to be won!!!"& "<br>")
for i=0 to 3
document.write(cars(i) & "<br>")
next
</script>
</body>
</html>
We saw most of this staff previously. The first thing this example does is; declares name and age variables. Then pops up input box asking the user a name, then another one asking age. It adds and multiples some numbers by the age. Then declares and populates an array. Document.write() method, writes the provided argument on the screen. & ties strings. You must use & when writing more then one variable or strings in one statement. Finally, it retrieves the array and writes on the screen using loops. Note that we can use html tag with vbscript statement for example document.write("hellow" & "<br>") writes hellow and breaks the line with html tag enclosed with.
.

Introduction Sub Procedure
Listed @ ConsumerVote.com - The Consumer Rated Web Directory
PC Articles
  Computer Safety
prevent viruses & Spyware