Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Presenter:
Duration: 50 min
Aim: To give an overview of the
features in VB Script.
What is VBScript?
•VBScript is a scripting language
•A scripting language is a lightweight programming language
•VBScript is a light version of Microsoft's programming language Visual Basic
Dynamic Arrays:
An array whose size changes during the time your script is
running. For a dynamic array, no size or number of
dimensions is placed inside the parentheses.
Eg. Dim MyArray()
ReDim AnotherArray()
To use a dynamic array, you must subsequently use ReDim
to determine the number of dimensions and the size of
each dimension.
Eg. ReDim MyArray(25)
The Preserve keyword preserves the contents of the array
during resizing.
Eg. ReDim Preserve MyArray(30)
There is no limit to the no. of times you can resize a dynamic
Operators & Precedence
Arithmetic:
– Exponentiation(^), Unary Negation(-)
– Multiplication(*), Division(/), Integer Division(\), Modulus(Mod)
– Addition(+), Subtraction(-)
– String Concatenation(&)
Comparison:
– Equlity(=), Inequality(<>)
– Less Than(<) , Less Than or Equal To(<=),
– Greater Than(>), Greater Than or Equal To(>=)
– Object Equivalence(Is)
Logical:
– Logical negation(Not)
– Logical conjunction(And)
– Logical disjunction(Or)
– Logical exclusion(Xor),
– Logical equivalence(Eqv), Logical implication(Imp)
*Each operation is evaluated as it occurs from left to right.
*The Is operator is an object reference comparison operator. It does not compare
objects or their values but only determines if two object references refer to
the same object.
Conditional Statements
If...Then...Else
To run only one statement for a True condition, use the
single-line syntax.
Eg. If myDate < Now Then myDate = Now
To run more than one line of code, you must use the
multiple-line (or block) syntax. This syntax includes the
End If statement
Eg. If value = 0 Then
AlertLabel.Font.Italic = True
End If
Do Loops
The statements are repeated either while a condition is True
or until a condition becomes True.
Eg. Do While myNum > 10
myNum = myNum – 1
counter = counter + 1
Loop
Eg. Do Until myNum = 10
myNum = myNum – 1
counter = counter + 1
Loop
While...Wend
Eg. while myNum <> 10
myNum = myNum – 1
counter = counter + 1
wend
*Because of the lack of flexibility in While...Wend, it is recommended that
you use Do...Loop instead.
For...Next
For loops, use a counter variable whose value increases or
decreases with each repetition of the loop.
You can use a For...Next statement to run a block of code, when
you know how many repetitions you want.
You can use a counter variable that increases or decreases with
each repetition of the loop, like this:
For i=1 to 10
some code
Next
Using the Step keyword, you can increase or decrease the counter
variable by the value you specify.
Eg . For j = 2 To 10 Step 2
total = total + j
Next
Step can be any integer. Default step is 1.
For Each…Next
This statement is used to iterate over a collection.
Dim names(2)
names(0) = "Tove"
names(1) = "Jani"
names(2) = "Hege"
For Each x In names
MsgBox x
You can exit a Do or a For loop by using the Exit
statement.
Eg. Do until i < 100
If i = a Then Exit Do
Loop
Eg. For I = 1 to 100
If i = a Then Exit For
Next
Introduction to Regular Expressions
A regular expression is a pattern of text that consists of
ordinary characters (for example, letters a through z) and
special characters, known as metacharacters. The
pattern describes one or more strings to match when
searching a body of text. The regular expression serves
as a template for matching a character pattern to the
string being searched.
Metacharacters in Regular
Expressions:
Quantifiers
* Matches the preceding subexpression zero or more times.
Eg. 'zo*' matches "z" and "zoo"
+ Matches the preceding subexpression one or more times.
Eg. 'zo+' matches "zo" and "zoo", but not "z".
? Matches the preceding subexpression zero or one time.
Eg. 'do(es)?' matches the "do" in "do" or "does".
Regular Expression (RegExp) Object
Provides simple regular expression support in VBScript.
• Global Property
Sets or returns a Boolean value indicating if a pattern should
match all occurrences in an search string or just the first.
• IgnoreCase Property
Sets or returns a Boolean value that indicates if a pattern
search is case-sensitive or not.
• Pattern Property
Sets or returns the regular expression pattern being searched
for. The pattern is delimited by double quotes(“).
Built in Functions
Array Function
Returns a Variant containing an array.
Eg. A = Array(10,20,30) ‘returns an array
InputBox Function
Displays a prompt in a dialog box, waits for the user
to input text or click a button, and returns the
contents of the text box.
Eg. Input = InputBox("Enter your name")
Join Function
Returns a string created by joining a number of substrings
contained in an array.
Join(list[, delimiter])
The default delimiter is space.
Eg. Join(myArray, “,”) ‘Returns a concantenated string of all the elements in the
array delimited by a comma .
Left Function
Returns a specified number of characters from the left side
of a string.
Left(string, length)
Eg. MyString = Left(“abcd”, 3) returns abc
Right Function
Returns a specified number of characters from the right side
of a string.
Right(string, length)
Eg. MyString = Left(“abcd”, 3) returns abc
Replace Function
Returns a string in which a specified substring has
been replaced with another substring a specified
number of times.
Eg. MyString = Replace("XXpXXPXXp", "p", "Y") ' A binary
comparison starting at the beginning ‘of the string.
Returns "XXYXXPXXY".
String Function
Returns a repeating character string of the length
specified.
Eg. MyString = String(5, "*") ' Returns "*****".
UCase Function
Returns a string that has been converted to
uppercase.
Eg. MyWord = UCase("Hello World") ' Returns "HELLO WORLD".
1.InStr--Returns the position of the first occurrence of one
string within another. The search begins at the first
character of the string.
Eg: Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
MyPos = Instr(4, SearchString, SearchChar, 1) ' A
textual comparison starting at position 4. Returns 6.
2.Mid-Returns a specified number of characters from a string
Dim MyString
MyString = "The dog jumps" ' Initialize string.
Mid(MyString, 5, 3) = "fox" ' MyString = "The fox jumps".
Mid(MyString, 5) = "cow" ' MyString = "The cow jumps".
VBScript Keywords
Empty : The Empty keyword is used to indicate an
uninitialized variable
LTrim-Removes value.
spaces on the left side of a string
False: Boolean false.
True: Boolean true.
Nothing : The Nothing keyword in VBScript is used to
disassociate an object variable from any actual object.
Eg. Set MyObject = Nothing
Null: The Null keyword is used to indicate that a variable
contains no valid data.