vb scripting tutorial

Upload: anand-priya

Post on 03-Apr-2018

259 views

Category:

Documents


4 download

TRANSCRIPT

  • 7/28/2019 Vb Scripting Tutorial

    1/37

    VB ScriptBy:

    Ali Imran Khan

    [email protected]

  • 7/28/2019 Vb Scripting Tutorial

    2/37

    VBScript is a scripting languageScripting language is a lightweight programminglanguage

    VBScript is a light version of Microsoft's programming

    language Visual Basic

    VBScript can be used for both client-side and server-sideprogramming.

  • 7/28/2019 Vb Scripting Tutorial

    3/37

    VBScript Variables A variable is a "container" for information you want to store.

    Naming Rules:

    Must begin with an alphabetic character

    Cannot contain a period

    Cannot exceed 255 characters

    Must be unique within its scope

  • 7/28/2019 Vb Scripting Tutorial

    4/37

    Lifetime OF Variables Local Variables:

    When you declare a variable within a procedure, the variable can

    only be accessed within that procedure. When the procedure

    exits, the variable is destroyed. You can have local variables withthe same name in different procedures, because each is

    recognized only by the procedure in which it is declared.

    Global Variables:

    If you declare a variable outside a procedure, all the procedureson your page can access it. The lifetime of these variables startswhen they are declared, and ends when the script is closed.

  • 7/28/2019 Vb Scripting Tutorial

    5/37

    Variable (Cont) Declaration:

    Dim Keyword is used to Declare a variable

    You can Assign a Value to variable directly (without declaringa variable). But not a good practice.

    Option Explicit Keyword is used to restrict variables to bedeclared before their usage.

    Example:Option explicit

    Dim var_ x

    var_x=1

  • 7/28/2019 Vb Scripting Tutorial

    6/37

    Constants

    Const keyword is used to declare constants.

    It is necessary to initialize the Constant during its

    declaration. You cannot change the value of constants in later

    script.

    Syntax:const x=1

  • 7/28/2019 Vb Scripting Tutorial

    7/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    8/37

    ArraysAn array is a set of variables conveniently packages for

    easy handling

    Sometimes you want to assign more than one value toa single variable. Then you can create a variable that

    can contain a series of values. This is called an arrayvariable

  • 7/28/2019 Vb Scripting Tutorial

    9/37

    Arrays (cont.) The declaration of an array variable uses parentheses ( )

    following the variable name.

    Example:

    dim names(2)

    names(0)=Ali

    names(1)=Imran

    names(2)=Khan"

  • 7/28/2019 Vb Scripting Tutorial

    10/37

    Arrays (cont.)An array can be multi dimensional.

    There can be 60 (maximum) dimensions in an array.

    Multiple dimensions are declared by separating thenumbers in the parentheses with commas.

  • 7/28/2019 Vb Scripting Tutorial

    11/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    12/37

    ProceduresA 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 that are passed to it by a callingprocedure

    without arguments, must include an empty set ofparentheses ()

  • 7/28/2019 Vb Scripting Tutorial

    13/37

    Procedures (Cont) Sub Keyword is Used to declare a procedure.

    End Sub Keyword is Used to defining the endingboundary of a procedure.

    Call Keyword is Used in order to invoke a procedure.

    Syntax:

    Sub mysub()some statements

    End Sub

    Call mysub()

  • 7/28/2019 Vb Scripting Tutorial

    14/37

    Procedures (Cont) Procedure can take arguments that are passed to it by

    calling that procedure .

    Syntax:

    Sub procedure name(arg1,arg2)

    some statements

    End Sub

    Call mysub(value1,value2)

  • 7/28/2019 Vb Scripting Tutorial

    15/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    16/37

    FunctionsA 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 callingprocedure

    without arguments, must include an empty set ofparentheses ()

    returns a value by assigning a value to its name

  • 7/28/2019 Vb Scripting Tutorial

    17/37

    Functions (Cont) Function Keyword is Used to declare a Function.

    End Function Keyword is Used to defining the endingboundary of a Function.

    is Used in order to invoke a Function.

    Syntax:

    Function myfunc()some statements

    End Function

    myfunc

  • 7/28/2019 Vb Scripting Tutorial

    18/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    19/37

    If Condition Ifkeyword is used for executing a set of code when certain

    condition is true.

    Endifkeyword is used to end the if condition code block.

    Syntax:

    If then

    Some statements

    End if

  • 7/28/2019 Vb Scripting Tutorial

    20/37

    If-Else Condition if...then...else - use this keyword if you want to select

    one of two sets of lines to execute

    if...then...elseif- use this keyword if you want toselect one of many sets of lines to execute

  • 7/28/2019 Vb Scripting Tutorial

    21/37

    If-Else Condition (cont.)

    Syntax :

    if thenSome statements

    Else

    Some statements

    end If

  • 7/28/2019 Vb Scripting Tutorial

    22/37

    If-Elseif Condition (cont.)

    Syntax :

    if thenSome statements

    Elseif then

    Some statements

    Else

    Some statements

    end If

  • 7/28/2019 Vb Scripting Tutorial

    23/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    24/37

    Select Case Condition Selectcasekeyword is used to execute one of many blocks of code.

    Syntaxselect case

    case Some statements

    case Some statements

    case Else

    Some Statementsend select

  • 7/28/2019 Vb Scripting Tutorial

    25/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    26/37

    For Loop For loop is used to execute set of statements , pre

    defined number of iterations.

    For Next keywords are used to implement the ForLoop.

    Syntax:

    For to Some Statements

    Next

  • 7/28/2019 Vb Scripting Tutorial

    27/37

    For Loop (cont.) Stepkeyword is used to increase or decrease the

    counter variable by the specified value.

    Syntax

    For To Step

    some statements

    Next

  • 7/28/2019 Vb Scripting Tutorial

    28/37

    For Loop (cont.)You can use for loop in order to repeat a block of code for

    each item in a collection, or for each element of an array.

    Syntax:

    For Each in

    Next

  • 7/28/2019 Vb Scripting Tutorial

    29/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    30/37

    Do-while loop Do-whilekeywords are used to execute specified code

    for a set of times (until a condition remains true or acondition becomes false).

    Syntax

    Do While Some Statements

    Loop

  • 7/28/2019 Vb Scripting Tutorial

    31/37

    Do-while loop (cont.)

    Do-Whilecan also used in following syntax:

    Do

    some Statements

    Loop While i>10

  • 7/28/2019 Vb Scripting Tutorial

    32/37

    Do-Until Loop Do Until keyword is used for repeating some set of

    statements until a certain condition is true.

    Syntax:

    Do Until

    some statmemts

    Loop

  • 7/28/2019 Vb Scripting Tutorial

    33/37

    Do-Until Loop (cont.) Do-Untilcan also used in following syntax:

    Dosome statements

    Loop Until

  • 7/28/2019 Vb Scripting Tutorial

    34/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    35/37

    Built in Functions

    VB Script provides several built in functions that canbe used just by calling them.

    Few Examples:

    Date

    Time Int

  • 7/28/2019 Vb Scripting Tutorial

    36/37

    Practical Work !

  • 7/28/2019 Vb Scripting Tutorial

    37/37

    Thank You !