How does mrScriptBasic compare to VBScript?
This topic summarizes some of the main differences in syntax between mrScriptBasic and VBScript.
For an overview of the features that are supported by mrScriptBasic but not by Visual Basic and VBScript, see
mrScriptBasic overview.
Option explicit
In mrScriptBasic, by default you must
explicitly declare variables using the
Dim statement. If you want variables to be declared
implicitly, you must add the
Option Implicit statement at the beginning of your script. However, this is not recommended. In contrast, in VBScript,
Option Implicit is the default. For more information, see
Variables.
Variable scope
When working in VBScript, a variable defined in the mrScriptBasic script is visible to Functions and Sub procedures. By default, this is not true in mrScriptBasic. To make mrScriptBasic behave in the same way as VBScript, you must include an
Option GlobalVariables statement in your script. For more information, see
Variables.
Array variables
In mrScriptBasic, you specify array variables by using
[] (brackets) instead of
() (parentheses) as you would in VBScript. This table provides some examples of specifying array variables in mrScriptBasic and VBScript. For more information, see
Variables.
Array
|
mrScriptBasic
|
VBScript
|
One dimension with 10 elements
|
Dim myArray[10]
|
Dim myArray(10)
|
Two dimensions vwith 5 and 10 elements respectively
|
Dim myArray[5][10]
|
Dim myArray(5, 10)
|
Dynamic array
|
Dim myArray[]
|
Dim myArray()
|
Array Sub declaration
|
Sub MySub(myArray[])
|
Sub MySub(myArray)
|
Unless Dim is used to define a variable as an array, an array value is treated as a categorical value. The elements of a categorical value are read only. For example, the following is valid because the array variable is defined as an array:
Dim MyArray[2]
MyArray[0] = "Hello"
MyArray[1] = " World"
However, the following is not valid because the value is a categorical:
Dim MyCategorical
MyCategorical = {1,2,3}
Debug.MsgBox(MyCategorical[0]) ' displays 1
MyCategorical[0] = 2 ' error - it is not possible to assign an element of a categorical
Removing an object reference from a variable
In mrScriptBasic, use Null instead of Nothing to remove an object reference. For example:
Set MyObject = Null
In mrScriptBasic, use the
IsNullObject function in the UNICOM Intelligence Function Library to test if an object reference no longer exists. For example:
If MyObject.IsNullObject() Then
' No object reference exists
...
End If
Default property expansion
mrScriptBasic uses [] (brackets) instead of () (parentheses) to reference an object in a collection using the collection’s default property (which is typically Item). This means that object lookup is consistent with the syntax used for MDM full names.
For example, to reference the age question in an MDM Document.Fields collection, using the default property, you use code similar to the following in mrScriptBasic:
LabelText = MyDocument.Fields["age"].Label
In VBScript, you would use code similar to the following:
LabelText = MyDocument.Fields("age").Label
In mrScriptBasic you can refer to objects in this manner even when
Item is a method rather than a property, provided it is the default member for the collection. However, generally you call methods using parentheses and not brackets. For more information, see
Call statements.
Parameters for object properties
Specify parameters for object properties in mrScriptBasic using [] (brackets) instead of () (parentheses) as in VBScript. To specify more than one parameter in mrScriptBasic, enclose each parameter in brackets instead of separating them with a comma as in VBScript. For example, the Text property of the MDM Label object has two parameters, user context and language. You specify the parameters in mrScriptBasic as follows:
myText = myObject.Labels["Label"].Text["Question"]["ENU"]
The equivalent line in VBScript is:
myText = myObject.Labels("Label").Text("Question", "ENU")
Call statements
mrScriptBasic always requires a call statement to be followed by () (parentheses), regardless of whether the method or function returns a value. In VBScript, parentheses are not required when calling a subroutine that does not return a value. For example, the following mrScriptBasic code calls the MDM Document.Save method:
MdmDoc.Save ("C:\Samples\myDocument.mdd")
The equivalent VBScript code would be:
MdmDoc.Save "C:\Samples\myDocument.mdd"
In VBScript, you can optionally prefix a call statement with the Call keyword. However, the Call keyword is not valid in mrScriptBasic.
Default method expansion
Unlike VBScript, mrScriptBasic does not expand calls to an object’s default method. For example, the default member of the ClearCase.ClearTool object is the CmdExec method. In VBScript, you can use the following syntax to call the method:
Set Cleartool = CreateObject("ClearCase.ClearTool")
sDiff = ClearTool(sCmd)
However, in mrScriptBasic you need to include the method name as follows:
Set Cleartool = CreateObject("ClearCase.ClearTool")
sDiff = ClearTool.CmdExec(sCmd)
Functions
mrScriptBasic uses the functions in the
UNICOM Intelligence Function Library, which has been designed for use by the market research industry. This means that some of the functions that are built into VBScript and Visual Basic are not available in mrScriptBasic. For more information, see
Visual Basic function equivalents.
Bitwise operations
In mrScriptBasic, you cannot use the And, Not, Or, and Xor operators for bitwise operations as you can in VBScript. However, you can use the bitwise functions in the UNICOM Intelligence Function Library. For more information, see
Bitwise operations.
Integer arithmetic
When the following code is executed in VBScript, x has a value of 2.4:
x = 12/5
When the same code is executed in mrScriptBasic, x has a value of 2. This is because 12 and 5 are both integers (type Long) and when you perform an arithmetic operation on two integers, the result is also an integer. If you want the result to be a real number (type Double), you must convert the values to real numbers, for example:
x = 12.0/5.0
The result is 2.4.
For more information, see
Arithmetic operators.
Concatenation operator
In mrScriptBasic you cannot use & (ampersand) to concatenate two strings together as you can in VBScript. Instead, you use + (plus sign). For example, the following line gives a syntax error in mrScriptBasic:
ErrorText = "Error : " & Err.Description
Instead, you concatenate the two strings by using the + operator:
ErrorText = "Error : " + Err.Description
When concatenating values using +, all of the values must be Text values. To convert other types of values to Text, use the
CText:
ErrorText = "Error : " + Err.Description + ", Line : " + CText(Err.LineNumber)
Alternatively, use the
MakeString function to ensure all of the values are converted to Text:
ErrorText = MakeString("Error : ", Err.Description, ", Line : ", Err.LineNumber)
Error handling sections
In mrScriptBasic, you can add an error-handling section to a script by using an On Error Goto <location> statement. For example:
On Error Goto Handler
' Main section of script where error might occur
...
Exit
Handler:
Debug.Log("Error on line " + CText(Err.LineNumber) + " : " + Err.Description)
Always include an
Exit statement immediately before the error-handling section. For more information, see
On Error.
Automatic calling of Err.Clear
Like VBScript, mrScriptBasic calls the Err.Clear method automatically whenever an On Error Resume Next statement is executed. However, unlike VBScript, mrScriptBasic does not call the Err.Clear method automatically when an Exit Sub or Exit Function statement is executed.
While statements
In mrScriptBasic, the end of a While statement is defined by
End While instead of VBScript’s
Wend keyword. For more information, see
While...End While.
Combining statements on one line
Unlike VBScript, in mrScriptBasic, you cannot combine multiple statements on a single line, and you cannot combine a GoTo label and a statement on one line.
See also