The VBScript Network and Systems Administrator's Cafe:

VBScript Statements

May 4 2009   5:10PM GMT

VBScript Statements: Explanation of the Execute Statement



Posted by: Jerry Lees
VBScript Statements, vbscriptstatements, VBScript, execute statement, execute

The VBScript Execute statement allows you to execute a series of VBScript statements as a block from within a VBScript Script. By itself it doesn’t sound terribly appealing… after all, Executing a series of VBScript statements is what you do inside a VBScript file.

However, if you consider that you can load the statements from another file, it becomes somewhat interesting because it could allow you to share code between scripts or add funtionality like some applications that allow scripting inside the application.

The syntax of the Execute Statement is simply as follows:

Execute statement

Statement can be a series of commands enclosed in quotes, with each command separated with a colon (:) or a variable that contains a string that is the commands you wish to execute.

For More information on this statement, check out Microsoft’s documentation here.

Sep 27 2008   8:19PM GMT

VBScript Statements: Explanation of the Private Statement



Posted by: Jerry Lees
VBScript Statements, Classes, Private

The Private statement is used to declare a variable as being Private inside a script. If used outside of a class it has little effect and the variable can be used throughout the script, however, if used inside a class statement it will only be able to be used inside the class statement itself.

The use of this statement works exactly like using Dim, except that the initialization of the variable must happen on a second line of code. Below is an example:

Private PrvVariable
PrvVariable = 10


Sep 26 2008   7:54PM GMT

VBScript Statements: Explanation of the On Error Statement



Posted by: Jerry Lees
VBScript Statements, Error control, on error

The on error statement allows you to somewhat control how the scripting engine handles errors in the scripts you write. It allows you to turn error handling on and off for the scripting engine so that you can handle them in your script.

 The Statement essentially has two ways it is used. First to turn off the script engine error checking:

On Error Resume Next

The second is to turn it on, thereby allowing the script engine to stop execution of a script when an error is encountered. Like so:

On Error Goto 0

One word of caution, It is very tempting to simply place “On error resume next” in a complex script block and not worry about annoying hard to find errors. However, It is generally better to try to find the source of the errors since ignoring the errors can cause unintended consequences– or at the very least difficult to find issues in another piece of code.

In general, if you use On Error Resume Next you should always check for errors and handle them in your code somehow. 


Sep 20 2008   8:10PM GMT

VBScript Statements: Explanation of the Property Get Statement



Posted by: Jerry Lees
VBScript Statements, Property, Property Get

Within a VBScript class the Property block allows you to Get the value of the property in the class. This statement works in conjunction with the Property Let and Property Get statements and can only be used inside a class inside VBScript– and no where else in the script.

 By default the Property Get statement is public but you can declare it to be private if you need to do so. Since this statement is used in a block, it is required that you end the block with the End Property statement. This statement works a little differently than the other two property statements, however, because it returns a value. The returning of a value works very much like a function, in that, you set the Name of the property to the value you want to return.

The block of code will look something like this:

Property Get PropertyName

 ’ code goes here
ProptertyName = value

End Property


Sep 19 2008   7:50PM GMT

VBScript Statements: Explanation of the Property Set Statement



Posted by: Jerry Lees
VBScript, VBScript Statements, Property, property set

Within a VBScript class the Property block allows you to set the value of the property in the class. This statement works in conjunction with the Property Let and Property Get statements and can only be used inside a class inside VBScript– and no where else in the script.

This would in turn be called in VBScript in your function after you create an instance of the class like so:

InstanceOfClass.PropertyName.Set (args)

 By default the Property Set statement is public but you can declare it to be private if you need to do so. Since this statement is used in a block, it is required that you end the block with the End Property statement. The block of code will look something like this:

Property Set PropertyName

 ’ Property setting code goes here

End Property


Sep 18 2008   7:31PM GMT

VBScript Statements: Explanation of the Property Let Statement



Posted by: Jerry Lees
VBScript, VBScript Statements, Classes, Property, Property Let

Within a VBScript class the Property block allows you to set the name of the property in the class. This statement works in conjunction with the Property Set and Property Get statements and can only be used inside a class inside VBScript– and no where else in the script.

 By default the Property Let statement is public but you can declare it to be private if you need to do so. Since this statement is used in a block, it is required that you end the block with the End Property statement. The block of code will look something like this:

Property Let PropertyName

 ’ code goes here

End Property


Sep 13 2008   11:11PM GMT

VBScript Statements: Explanation of the Function Statement



Posted by: Jerry Lees
VBScript, Functions, VBScript Statements, VBScript Functions

The Function statement creates a function and allows you to list the arguments (if any) which are to be passed into the function.
 
The major difference between a function and a subroutine, is that a function can return a value. To return a value you use the function name as if it were a already declared variable and simply set the name of the function to the value you want to return. However, you do not not have to assign or Set a return value. 
 
Functions can be declared Private or Public (the default). You can drop out of a function, without exiting the script entirely by using the Exit statement. You can call other functions or subroutines from within a function (nesting). You must end every function with End Function or you will get an error message.

Here is an example of a function and it’s use:

wscript.echo add(2,3)

function add(x , y)

       add = x+4
end Function


Sep 6 2008   11:41PM GMT

VBScript Statements: Explanation of the Public Statement



Posted by: Jerry Lees
Development, VBScript, VBScript Statements, Variable Types, Public

The VBScript Public statement creates a variable, function, subroutine, or class that is available outside the bounds of the scope it was created in and it functions very much like a global variable, except that it can be declared or initialized inside a non0-global code block.

 An example would be:

Public TestVariable


Aug 29 2008   2:04PM GMT

VBScript Statements: Explanation of the Randomize Statement



Posted by: Jerry Lees
VBScript Statements, randomize, Random Numbers

The Randomize statement seeds the VBScript random number generation engine with a new seed. A seed is a user defined value that helps create a random number.

There are no options for the Randomize statement, it’s use is simply:

Randomize


Aug 17 2008   2:02AM GMT

VBScript Statements: Explanation of the ReDim Statement



Posted by: Jerry Lees
arrays, VBScript Statements, dimensional arrays, redim

The ReDim statement in VBScript allows you to declare a array, or a multi-dimensional array, in your code. Also, this statement allows you to re-declare the number of elements in the array at a later time in the code.

This is important because it also allows you to release memory or allocate more memory for use in your script.

As you use this statement, be sure to use the Preserve keyword if you wish to preserve the existing data in the array.

Below are examples:

Dim x(10)
ReDim x(15)