vb scripting basics

23
VBScripting Basics 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 VBScript is only supported by Microsoft's browsers (Internet Explorer)

Upload: online-it-training

Post on 22-Jan-2015

202 views

Category:

Education


9 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Vb scripting basics

VBScripting BasicsWhat is VBScript?VBScript is a scripting languageA scripting language is a lightweight programming languageVBScript is a light version of Microsoft's programming language Visual BasicVBScript is only supported by Microsoft's browsers (Internet Explorer)

Page 2: Vb scripting basics

Data types

VBScript supports only one data type called ‘Variant’

The variant data type is a special kind of data type that can contain different kinds of information

It is the default data type returned by all functions in VBScript

A variant behaves as a number when it is used in a numeric context and as a string when used in a string context.

It is possible to make numbers behave as strings by enclosing them within quotes.

Page 3: Vb scripting basics

VBScript Variables

As with algebra, VBScript variables are used to hold values or expressions.

A variable can have a short name, like x, or a more descriptive name, like carname.

Rules for VBScript variable names: Must begin with a letter  Cannot contain a period (.) Cannot exceed 255 characters In VBScript, all variables are variant data type,

that can store different types of data.

Page 4: Vb scripting basics

Declaring (Creating) VBScript Variables

Creating variables in VBScript is most often referred to as "declaring" variables.

You can declare VBScript variables with the Dim, Public or the Private statement. Like this:

Dim xDim carname

Page 5: Vb scripting basics

VBScript Array VariablesAn array variable is used to store multiple

values in a single variable.In the following example, an array containing 3

elements is declared: Ex: Dim names(2)The number shown in the parentheses is 2. We

start at zero so this array contains 3 elements. This is a fixed-size array. You assign data to each of the elements of the array like this:

names(0)="Tove"names(1)="Jani"names(2)="Stale"

Page 6: Vb scripting basics

Option Explicit You can also declare a variable implicitly by

simply using its name in your script. That is not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in your script.

Note: Variables declared with Dim at the script level are

available to all procedures within the script. At the procedure level, variables are available only within the procedure.

Page 7: Vb scripting basics

VBScript Procedures

VBScript has two kinds procedures: Sub procedure Function procedure Sub procedure: is a series of statements, enclosed by the Sub

and End Sub statements can perform actions, but does not return a

value can take arguments Ex: Sub kumar() msgbox "kumar" End Sub kumar()

Page 8: Vb scripting basics

VBScript Function Procedures

A Function procedure: is a series of statements, enclosed by the Function

and End Function statements can perform actions and can return a value can take arguments that are passed to it by a calling

procedure without arguments, must include an empty set of

parentheses () returns a value by assigning a value to its name Ex: function myfunction()

  myfunction=Date()end function

Page 9: Vb scripting basics

Conditional Statements

Conditional statements are used to perform different actions for different decisions.

In VBScript we have four conditional statements:

If statement - executes a set of code when a condition is true

If...Then...Else statement - select one of two sets of lines to execute

If...Then...ElseIf statement - select one of many sets of lines to execute

Select Case statement - select one of many sets of lines to execute

Page 10: Vb scripting basics

If...Then...Else

Use the If...Then...Else statement if you want to execute some code if a condition is true select one of two blocks of code to execute If you want to execute only one statement when a condition

is true, you can write the code on one line:

Ex: Function greeting()

i=hour(time)

msgbox i

If i < 10 Then

msgbox "Good morning!"

Else

msgbox "Have a nice day!"

End If

End Function

greeting()

Page 11: Vb scripting basics

If...Then...ElseIf

You can use the If...Then...ElseIf statement if you want to select one of many blocks of code to execute:

Ex: Function greeting()

i=hour(time)

If i = 10 Then

msgbox "Just started...!"

ElseIf i = 11 then

msgbox "Hungry!"

ElseIf i = 12 then

msgbox "Ah, lunch-time!"

ElseIf i = 16 then

msgbox "Time to go home!"

Else

msgbox "Unknown"

End If

End Function

greeting()

Page 12: Vb scripting basics

Select Case

You can also use the "Select Case" statement if you want to select one of many blocks of code to execute:

Select Case d  Case 1   msgbox "Sleepy Sunday"  Case 2    .write("Monday again!")  Case 3    document.write("Just Tuesday!")  Case 4    document.write("Wednesday!")  Case 5    document.write("Thursday...")  Case 6    document.write("Finally Friday!")  Case else    document.write("Super Saturday!!!!")End Select

Page 13: Vb scripting basics

Looping StatementsWhile...Wend. Performs a series of statements as long as a specifiedcondition is True.➤ For...Next. Uses a counter to perform a group of statements a specifiednumber of times.➤ Do...While. Performs a series of statements indefinitely, as long as a specifiedcondition is True.➤ Do...Until. Performs a series of statements indefinitely, until a specifiedcondition becomes True.

Page 14: Vb scripting basics

Looping Statements Looping statements are used to run the same block of code a specified

number of times. In VBScript we have four looping statements: For...Next statement:runs code a specified number of times Ex1: for i=1 to 5 Msgbox I Next The Step Keyword With the Step keyword, you can increase or decrease the

counter variable by the value you specify. In the example below, the counter variable (i) is INCREASED by

two, each time the loop repeats To decrease the counter variable, you must use a negative

Step value. You must specify an end value that is less than the start value.In the example below, the counter variable (i) is DECREASED by two, each time the loop repeats.

Ex2: for i=1 to 5 step 2 Msgbox I Next Ex3: for i=5 to 2 step - 1 Msgbox I Next

Page 15: Vb scripting basics

Exit a For...Next

You can exit a For...Next statement with the Exit For keyword.

For i=1 to 5If i=3 ThenExit forEnd Ifmsgbox iNext

Page 16: Vb scripting basics

Do...Loop If you don't know how many repetitions you want,

use a Do...Loop statement. The Do...Loop statement repeats a block of code

while a condition is true, or until a condition becomes true.

Repeat Code While a Condition is True You use the While keyword to check a condition in a

Do...Loop statement Ex: Do While i>10

  some codeLoop

Note:If i equals 9, the code inside the loop above will never be executed.

You use the Until keyword to check a condition in a Do...Loop statement.

Ex:Do Until i=10  some codeLoop

If i equals 10, the code inside the loop will never be executed.

Page 17: Vb scripting basics

VBScript Operators Operators are used to "do operations" or manipulate

variables and values. For example, addition is an example of a

mathematical operator and concatenation is an example of a string operator.

The plus sign "+" is the operator used in programming language to represent this mathematical addition operation.

Page 18: Vb scripting basics

Math Operators When you want to perform addition, subtraction,

multiplication, and other mathematical operations on numbers and variables use the operators listed below.

Page 19: Vb scripting basics

ComparisonWhen you want to compare two numbers to see which is bigger, if they're equal, or some other type of relationship use the comparison operators listed below. Common uses of comparison operators are within conditional statements like an If Statement or the condition check in a While Loop.

Page 20: Vb scripting basics

ComparisonEx:

Page 21: Vb scripting basics

Logical operatorsLogic operators are used to manipulate and

create logical statements. For example if you wanted a variable shoeSize to be equal to 10 or 11 then you would do something like:

Dim shoeSize shoeSize = 10 If shoeSize = 10 Or shoeSize = 12 Then msgbox "RightSheSize"End If

Page 22: Vb scripting basics

Logical operatorsEx:

Page 23: Vb scripting basics

VBScript String Concatenation OperatorWhen you have various strings that you would like to combine into one string use the concatenation operator.