Difference between revisions of "VBScript other features"
(→Identifiers in square brackets: fix link) |
m (1 revision imported) |
(No difference)
|
Latest revision as of 08:20, 1 December 2017
VBScript features variables, operators, functions, conditional statements, loops, and classes.
This page discusses features not mentioned in the above links.
Contents
With
blocks
A With
block allows you to evaluate a function or variable to an object in order to access the object multiple times.
Say you are doing multiple things to an object:
Set Dict = CreateObject("Scripting.Dictionary") Dict.Add key, item AddQ Dict.Count Dict.Item(oldkey) = NewItem
You can simplify the above slightly to:
Set Dict = CreateObject("Scripting.Dictionary") With Dict .Add key, item AddQ .Count .Item(oldkey) = NewItem End With
Wherever there is a .
without an object expression on the left, the object expression next to the beginning With
will be evaluated and used.
With CreateObject("Scripting.Dictionary") .Add key, item AddQ .Count .Item(oldkey) = NewItem End With
This next example illustrates that With
will only evaluate the expression once, and will use the same object for all object accesses inside the block. Note that doing the second step will make it impossible to set the object to Nothing
at a later time, or access the object after the End With
.
You can embed With
blocks. The innermost object will be accessed in an embedded With
.
With CreateObject("Scripting.FileSystemObject") Path = .BuildPath(GetWorkingDirectory(), "hi.txt") With .OpenTextFile(Path, 2, True) .WriteLine "hi there" .Close End With End With
Here, .WriteLine
and .Close
access the created text stream object, and .BuildPath
and .OpenTextFile
access the created file system object. Note that neither of these objects can be set to Nothing
at a later time, or be accessed after the End With
.
ByVal
and ByRef
parameters
When you pass a variable to a method (function, subroutine, or property), a "reference" to the original variable is passed to the method to be manipulated. When the method exits, if the value of the parameter variable was changed the change will be reflected on the original variable in the calling method.
To show this we have a function called Changer
which will accept variable X
and call it with our variable:
Sub Changer(X) X = 10 End Sub ... Dim Var1 Var1 = 1 Changer X AddChat vbGreen, X
The result is the number 10 which was assigned inside the function.
Normally, this is alright, but what if we do not want the variable outside changed? We can define the parameter as ByVal
("by value") to change this behavior:
Sub Changer(ByVal X) X = 10 End Sub ... Dim Var1 Var1 = 1 Changer X AddChat vbGreen, X
The result this time will be 1, the original value. The variable's value will be copied when entering the function and ignored when the function exits.
If the argument passed to the function was a literal or the result of another function or property, only the value will be passed to the function, effectively "by value".
The default, implied behavior is ByRef
("by reference"), though using this keyword only emphasizes to the reader the purpose.
Objects are always stored as a reference, so ByVal
will not copy the object, but the reference to the object (which is still the object). Changes to an object passed to a function as a parameter will always affect the original object.
Splitting lines with underscore
Normally it is a good idea to keep your script readable by not writing long, convoluted lines with many operations.
If this is not possible, it is possible to make a statement span multiple lines by ending one line with an underscore (_
) and continuing the statement on the next line. You may break the statement anywhere there is a space. A line can be broken up into every word or operator but that would become annoying to read. _
must be the last character of the line and there must be a space before it. This operator is ignored inside comments.
' old line Var = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f") ' new lines Var = Array("0", "1", "2", "3", "4", "5", "6", _ "7", "8", "9", "a", "b", "c", "d", _ "e", "f")
Some scripters break up lines when the start to line-wrap or at a character length such as 80 characters, 100 characters, or 120 characters (some text editors such as Notepad++ even allow you to see how wide that is).
Combining lines with :
The "opposite" operator to _
is :
. Some versions of Basic require every line to begin with :
. In Visual Basic and VBScript, however, this is optional and pointless. However, if you want to place two statements on the same line, you can place a :
after the first statement and put another statement after it. This operator is ignored inside comments.
' old lines Dim Var Var = 1 ' new line Dim Var : Var = 1
In many cases, :
can make your code less readable, but in some cases it can help your code and shorten it. For example, if you want to declare and then assign a default for a value in the same line as shown.
Identifiers in square brackets
Identifier names can contain A through Z, 0 through 9, and/or _ but cannot start with 0 through 9. Identifiers cannot be the names of any of the built-in keywords or constants.
Identifiers can also be placed inside square brackets ([
...]
). Almost anything can be placed inside square brackets and considered a valid variable, method, property, or class name, including spaces, nothing at all, and keywords. Only newlines and ]
s cannot be used. _
cannot be used to break inside an identifier.
This is valid VBScript.
Dim [], [ ], [die!], [Hello,], ["Inigo Montoya"], [my name is], [prepare], [To], [you killed my father] [] = 1 [ ] = 2 AddQ [] + [ ] [die!] = "die!" [Hello,] = "Hello, " ["Inigo Montoya"] = "Inigo Montoya" [my name is] = "my name is " [prepare] = "prepare " [To] = "to " [you killed my father] = ". You killed my father, " AddQ [Hello,] & [my name is] & ["Inigo Montoya"] & [you killed my father] & [prepare] & [To] & [die!]
The identifier [prepare]
in this case is equivalent to the identifier prepare
, so this can't be used to avoid conflicting identifiers of the same name.
Try to avoid using this beyond its obvious comedic effect, as it can get quickly annoying to debug.
What you can't do with VBScript
Many things available to other languages are not available to VBScript. Here we will list all of the things Visual Basic 6 provides that cannot be done with VBScript.
Optional
parameters. All functions must require all arguments to be passed in VBScript. However, functions defined in VB6, such as those provided in the script functions, can have optional arguments. Optional arguments may be omitted from calls to functions such as AddQ():
AddQ "hi" AddQ "hi", , "tag" '(You CAN do this, since AddQ is available via VB6)
ParamArray
keyword. You cannot create a parameter array in VBScript. A parameter array is a parameter keyword (in place of ByVal
or ByRef
) which can make a function appear to accept an unbounded amount of arguments, which will be stored into an array. You can call functions such as AddChat() that accept a parameter array.As
keyword. All variables defined in VBScript are actually defined as type "Variant" and can accept any value type. Using the As
keyword will result in an error.Like
operator. Use the Match() function provided by StealthBot to access the Like
operator.GoTo
statement. In VB6, this can be used to make execution "go to" another place in the code based on "labels" (also not available). This is still partially used in error handling, but it cannot be used alone (it is also an an avoided feature in many schools of thought).AddressOf
operator. This is used in VB6 to get a operand's (such as a function) memory location, so that API calls can use it to call that function. This is not necessary or available in VBScript.Let
keyword for assignment. This keyword emphasizes that you want to store a value into a variable in an assignment operation (in place of the Set
keyword for assignment), but it is optional in VB6 and not available in VBScript.Declare Function
statement. This is used in VB6 to declare a function which references a Windows API. VBScript cannot do this, but the Script Support Class provides several function "wrappers" that allow scripts to call the function "remotely", through the VB6 code.