arrays in VBScript

Posted on February 26th, 2008 in VBScript by admin

arrays in VBScript

Arrays

What if you want to declare numerous values to a single variable . This is when we use an array . To declare an array we use the same syntax as a normal variable declaration but add ( ) on the end .

Here is an example

dim grades(4)

How many elements do you think we have created here is it 4 , in fact we have created 5 because arrays are zero based . This is an example of a fixed size array.

We can assign data to the array like this

grades(0) = 50
grades(1) = 80
grades(2) = 94
grades(3) = 12
grades(4) = 25

You can retrieve the data like this

myGrade = grades(0)

which would store the value 50 in myGrade

You can have more than one dimension in an array , for example here is a 2 dimensional array

Dim tictactoe(2,2)

this would create an array with 3 columns and 3 rows.

The total amount of dimensions you can have is 60

variables in VBScript

Posted on February 26th, 2008 in VBScript by admin

variables in VBScript

Variables

Before you use a variable in VBScript you should declare its name . To do this in VBScript you use the dim keyword like the following examples.

You should follow a few rules when you declare variable names and these are

  1. must begin with a letter
  2. cannot contain a period (.)
  3. cannot have more than 255 characters

dim x
dim y
dim myAge
dim myWage

You could have put these all on the same line like this

dim x , y , myAge , myWage

The initial value of the variable is empty in VBScript

To prevent mistakes when declaring variables you can use Option Explicit before declaring them and this means you must declare all your variables with dim , public or private , if you dont you will get an error . Anyone who has created scripts in JavaScript will see the advantage here where you can declare a variable like this

myvar

but if you make a spelling mistake like myyar later on this will not be flagged and can lead to some strange and hard to find errors.

Types of Variables

In VBScript a variable can hold any a value of any data type and this data type does not have to be set when the variable is declared . This means you can change the data type of your variable in your program .

Datatype Description
Empty Variant is uninitialized . Value is 0 for number variables and a zero length string “” for empty strings
Null Variant which contains no valid data
Boolean either True or False
Byte integer in the range 0 to 255
Integer integer in the range -32768 to 32767
Currency -922,337,203,685,477.5808 to 922,337,203,685,477,5807
Long an integer value in the range -2,147,483,648 to 2,147,483,647
Single single precision floating point number in the range of 3.402823E38 - 45 to 1.401298E - 45 for negative values and 1.401298E - 45 to 3.402823 E 38 for positive values
Double a double precision floating point number
Date/Time a number that represents the date between Jan 1 100 to Dec 31 9999
String a variable length string that can contain 2 billion characters
Object contains an object
Error contains an error number

procedures in VBScript

Posted on February 26th, 2008 in VBScript by admin

procedures in VBScript

Procedures

There are two types of procedures in VBScript , Sub procedures or Function procedure . If you do not need to return a value you can use the Sub procedure but if your statements need to return a value then use a Function.

Sub Procedures

a sub procedure is a statement / series of statements that are enclosed by the Sub and End Sub statements
a sub procedure cannot return a value but it can perform certain actions
If you do not have any arguments you use ( )
Can take arguments that are passed to it by a calling procedure

Sub samplesub ( )
collection of statements here
End Sub

Example:

sub message() document.write(”this is a VBScript Sub procedure”) end sub call message()Here is the code for the above example above

This code would go in the <HEAD> section of your page ideally

<script language=”VBScript”>
sub message()
document.write(”this is a VBScript Sub procedure”)
end sub

And to call this sub procedure we would use the following script

<script language=”VBScript”>
call message()
</script>

Function Procedures

a function procedure is a statement / series of statements that are enclosed by the Function and End Function statements
a function procedure can return a value and it can perform certain actions
If you do not have any arguments you use ( ) on their own
Can take arguments that are passed to it by a calling procedure
it can return a value by assigning a value to its name

Function samplefunction(  )
collection of statements here
samplefunction = somevalue
End Function

Example

function message1() message1 = “a sample VBScript function” end function document.write “here is ” & message1() Here is the code for the example aboveThis would go in the <HEAD> section

<script language=”VBScript”>
function message1() message1 = “a sample VBScript function”
end function
</script>

This code would call the function

<script language=”VBScript”> document.write “here is ” & message1() </script>

Conditional Statements

Posted on February 26th, 2008 in VBScript by admin

Conditional Statements

If you want to process a different series of tests and act based on the results of these tests, there are a couple of ways you can go about this using Conditional statements . We have a choice of 2 statements in VBScript If..Then…Else and Select…Case .

If…Then…Else

You use this if you have one of two lines of code you need to execute . There are a variety of formats that this statement can take lets have a look at some of them .

If tme > 12 Then document.write(”hello good afternoon/evening”)

tme = hour(time) If tme > 12 Then document.write(”good evening”)This example is fine if you only wanted to test that the condition is true , i.e the hour is greater than 12 but if the hour is not greater than 12 nothing is displayed on the screen. So if you want to execute only one statement when a condition is true, this is the correct way to do it .

If tme < 12 then
document.write(”Good Morning”)
Else
document.write(”Good Afternoon / Evening”)
End If

If you want to execute some statements if a condition is true and execute others if a condition is false then the above syntax is what you would use , note this is better than the previous example because a message would be displayed on the screen all the time . The first block of code will be executed if the condition is true if tme < 12 then Good Morning is printed on the screen , the other block will be executed if the condition is false Good Afternoon / Evening.

You can also use ElseIf to make your code more readable like the following example

If (grade > 90 ) then
document.write(”Well done , a great score”)
ElseIf (grade > 75) then
document.write(”Good try”)
End If

in the above example we have only one ElseIf statement but we could have several . You can also nest If statements inside each other , like this .

If(grade1 > 90) then
If(grade2 > 85 ) then
statements would go here
If(grade2 > 75 ) then
statements would go here
End If
End If
Select Case

When a variable can take a number of different values then a Select Case statement can come in very handy . This has obvious advantages over the If statement in that it is far more readable .

Select case variable
case 1
statements to be executed 1
case 2
statements  to be executed 2
case 3
statements to be executed 3
case else
default statement
End Select

First we have a single expression usually a variable, that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed . It is good programming practice to supply a default case else even if you know that this will never execute .

A case can also take multiple values and strings like the following examples

case 10 , 20 , 30
case “programmershelp”