Jul 3 2008 2:55AM GMT
Posted by: Jerry Lees
VBScript,
Developer documentation,
VBScript Statements,
vbscriptstatements,
REM
The VBScript REM statement is used to place a comment in your script or function. It is typically considered essential to place comments in code, not only to document what something does for others– but for yourself 6 months down the line.
Another short hand method of placing a comment in the code is to use the ‘ character to comment the code. Below are two examples:
Rem This is a comment
‘ This is a comment too
x=x+1 ‘this is a comment after a line of code, the code is processed and the comment is ignored.
Jul 2 2008 12:06AM GMT
Posted by: Jerry Lees
VBScript Statements,
vbscriptstatements,
Conditional statements,
working with arrays,
for,
next,
for each,
for each next
The For Each Next statement combination is very useful for situations where you need to loop through a piece of code a specific number of times, just like the For Next statement, except that you may not even know at run time how many items you need to loop through.
This particular variation of the For Next statement is particularly helpful when iterating through arrays, objects, or collections– cases where you may not know how many items are in the object or collection with out going through them first. (Which would be a waste of resources)
You can use Exit For statements to exit out of a For Each Next loop, if you need to check for a condition inside the loop.
Here is an example of a For Each Next statement:
ReDim Collection(2)
Collection(0) = “Test”
Collection(1) = “Test 2″
Collection(2) = “Test 3″
For Each Item in Collection
wscript.echo item
next
Jun 28 2008 11:20PM GMT
Posted by: Jerry Lees
VBScript,
VBScript Statements,
vbscriptstatements,
Conditional statements,
for,
next,
for next,
step
The For Next statement is very useful for situations where you need to loop through a piece of code a specific number of times.
You can use Exit For statements to exit out of a For loop, if you need to check for a condition inside the loop. Additionally, The Step keyword at the end of the line that has the for statement will allow you to loop through a For statement in any size of increment.
Here is an example of a for statement, followed by one with a Step key word:
For x = 1 to 10
wscript.echo x
next
For x=1 to 20 step 2
wscript.echo x
next
Jun 20 2008 3:19PM GMT
Posted by: Jerry Lees
VBScript,
VBScript Statements,
vbscriptstatements,
erase,
working with arrays,
dimensional arrays
The VBScript Erase statement is used to erase all the values of an array, but keep the array size and dimensions intact.
This is useful when you re-use an array and you want to ensure the values inside an array have truely been cleared and no old data is left in the array.
For example, the following would print a blank line:
Redim Bob(10)
Bob(1) = “Rickky Bobby”
Erase Bob
Wscript.echo Bob(1)
Jun 18 2008 1:14PM GMT
Posted by: Jerry Lees
VBScript,
VBScript Statements,
vbscriptstatements,
exit
The Exit statement in vbscript allows you to exit a block of code, in the case of a subroutine (Exit Sub) or a function (Exit Function), or the entire script.
This statement is assumed at the end of a script, and therefore is not required, but is typically used in conditional statements to exit a section of code when a particular condition is met.
Below is an example:
mysub(1)
mysub(3)
mysub(2)
mysub(0)
sub mysub(testvar)
testvar=testvar + 1
if testvar =2 then
exit sub
else
wscript.echo testvar
endif
end sub
Jun 14 2008 10:19PM GMT
Posted by: Jerry Lees
VBScript,
while,
VBScript Statements,
vbscriptstatements,
do loop,
until,
do while,
do until,
Conditional statements
The Do … Loop statement is very useful (like, for … next and Select … case) to execute a block of code more than once. However, where it differs from the other two is that the Do … Loop statement executes the code as long as (while) a condition is true or until a condition becomes true.
Typical uses of a Do … Loop are in cases where you may not know how many times you need to execute the code section because it could vary from situation to situation, like when reading in a file or taking in keyboard input.
In this example of a Do While Loop, we check to see if a variable is less than 10 and if not continue to run the code in the loop. (Notice how when x = 10 the code does not run and at the end x =10?)
x = 0
Do While x> 10
Wscript.Echo(”X = ” & x)
x= x + 1
Loop
Wscript.Echo(”X is now ” & x)
In this example of a Do Until Loop, we check to see if a variable is greater than 10, but use the until condition to run the loop. (Notice how when X = 10 the code does run and at the end x=11?)
x = 0
Do Until x > 10
Wscript.Wcho(”X = ” & x)
x= x + 1
Loop
Wscript.Echo(”X is now ” & x)
May 26 2008 3:48PM GMT
Posted by: Jerry Lees
VBScript,
VBScript Statements,
vbscriptstatements,
option,
option explicit
In VBScript declaring your variables up front is not required (as stated in this sites explanation of the Dim Statement), however, many programmers choose to declare their variables up front to avoid having to track down misspellings in their code. They go further by adding the Option Explicit statement to force them to do so. With Option Explicit turned on and error is thrown when a variable is used before being declared.
Consider the following code, that doesn’t use Option Explicit:
TheFirstVariable = 1
TheSecondVariable = 2
TheThirdVariable = TehFirstVariable + TheSecondVariable
Wscript.echo TheThirdVariable
The output will be 2, when you were expecting 3! To find the error, if you didn’t notice it, read the third line very slowly TheFirstVariable is spelled differently as TehFirstVariable– therefore creating a new variable with a default value of zero. (or empty if used as a string)
May 24 2008 3:47PM GMT
Posted by: Jerry Lees
VBScript,
VBScript Statements,
vbscriptstatements,
DIM,
DIM Statement
The DIM statement is used to declare variables prior to being used. To save space in your code, you can declare more than one variable in a single statement by separating each variable with a comma.
For example:
Dim x
Dim x, y, z
The Dim statement is not required in VBScript because VBScript will automatically create variables at the time you first use them.
May 23 2008 8:00PM GMT
Posted by: Jerry Lees
VBScript,
VBScript Statements,
vbscriptstatements,
Const,
Const statement
The Const statement is useful when you want to reference a specific, unchanging, value in your code multiple times without typing it over and over.
For example, in order to use the Const Statement to create a reference to the value of pi to 50 decimal places, you use the following code:
Const pi = 3.14159265358979323846264338327950288419716939937510
The important thing to remember is that, constants can not be changed once set in your code– Doing so will result in an error being thrown.