ARRAYS
What is an array? • We know very well that a variable is a container to store a value. Sometimes, developers are in a position to hold more than one value in a single variable at a time. When a series of values is stored in a single variable, then it is known as an array variable.
CREATING ARRAYS • We can create arrays several ways, depending on whether they are static or dynamic. • Static arrays - Static arrays stay a fixed size throughout their lifetime— that is, the index size remains constant. Thus, when we create a static array, we must know how many items the array will contain throughout its lifetime. • Dynamic arrays – Suppose if we don't know the number of items or don’t know that the array's index size will change, we need to create a dynamic array. Dynamic arrays don't have a fixed index size. We can increase or decrease the index size at any time.
Array Declaration and Assigning Values to an Array Example 1: option explicit dim arr(3) arr(0)=1 arr(1)="2" arr(2)="sun" arr(3) = #10/07/2013# msgbox arr(0) msgbox arr(1) msgbox arr(2) msgbox arr(3)
Example 2: option explicit Dim arr arr=array("5",100,45,464) msgbox arr(0) msgbox arr(1) msgbox arr(2) msgbox arr(3)
Multi-Dimension Arrays • Arrays are not just limited to single dimension and can have a maximum of 60 dimensions. Two-dimension arrays are the most commonly used ones. Example: Option explicit Dim arr(1,2) //2 rows and 3 columns arr(0,0) = "A" arr(0,1) = "B" arr(0,2) = "C" arr(1,0) = "Dr" arr(1,1) = "E" arr(1,2) = "F" msgbox arr(0,0) msgbox arr(0,1) msgbox arr(0,2) msgbox arr(1,0) msgbox arr(1,1) msgbox arr(1,2)
ReDim Statement • ReDim Statement is used to declare dynamic-array variables and allocate or reallocate storage space. Example: option explicit dim arr() redim arr(5) arr(0)=1 arr(1)=2 arr(2)=3 arr(3)=4 arr(4)=5 redim preserve arr(10) arr(6)=7 msgbox arr(0) msgbox arr(1) msgbox arr(6) redim preserve arr(4) msgbox arr(0) msgbox arr(1) msgbox arr(2) msgbox arr(3) msgbox arr(4)
Array Methods LBound Function • The LBound Function returns the smallest subscript of the specified array. Hence, LBound of an array is ZERO. Example: option explicit dim arr(5) arr(0)=1 arr(1)=2 arr(2)=3 arr(3)=4 arr(4)=5 msgbox lbound(arr)
UBound Function The UBound Function returns the Largest subscript of the specified array. Hence, this value corresponds to the size of the array. Example: option explicit dim arr(5) arr(0)=1 arr(1)=2 arr(2)=3 arr(3)=4 arr(4)=5 msgbox ubound(arr)
Split Function • A Split Function returns an array that contains a specific number of values split based on a Delimiter. Example: option explicit dim arr,b,c,i arr=split("sun & technology & integrators","&") b=ubound(arr) for i=0 to b msgbox arr(i) next
Join Function • A Function, which returns a String that contains a specified number of substrings in an array. This is an exact opposite function of Split Method. Example: option explicit dim arr,b,c arr=array("sun","technology","integrators") b=join(arr) msgbox b c=join(arr,0) msgbox c
Filter Function A Filter Function, which returns a zero-based array that contains a subset of a string array based on a specific filter criteria. Example: option explicit Dim MyIndex Dim MyArray (3) MyArray(0) = "Sunday" MyArray(1) = "Monday" MyArray(2) = "Tuesday" MyIndex = Filter(MyArray, "Mon") msgbox myindex(0)
IsArray Function The IsArray Function returns a Boolean value that indicates whether or NOT the specified input variable is an array variable. Example: option explicit dim a,b a=array("Red","Blue","Yellow") b = "12345" msgbox isarray(a) msgbox isarray(b)
Erase Function The Erase Function is used to reset the values of fixed size arrays and free the memory of the dynamic arrays. Example: option explicit dim a(2) a(0)="hello" a(1)=22 a(2)=08 msgbox a(0) msgbox a(1) msgbox a(2) erase a msgbox a(0) msgbox a(1) msgbox a(2)
FUNCTIONS
What is a Function? A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing same code over and over again. This will enable programmers to divide a big program into a number of small and manageable functions. • Function Definition Before we use a function, we need to define that particular function. The most common way to define a function in VBScript is by using the Function keyword, followed by a unique function name and it may or may not carry a list of parameters and a statement with an End Function keyword
Example 1: option explicit Function Hello() msgbox("Hello") End Function call hello()
Example2: option explicit Function Hello(name,age) msgbox( name & " is " & age & " years old.") End Function call hello("vb",4)
Example3: option explicit Function sum(number1,number2) sum = number1 + number2 End Function Dim total total = sum(100,9) msgbox total
Sub-Procedures Sub-Procedures are similar to functions but there are few differences. • Sub-procedures DONOT Return a value while functions may or may not return a value. • Sub-procedures Can be called without call keyword. • Sub-procedures are always enclosed within Sub and End Sub statements.
Example: option explicit sub Hello() msgbox("Hello") End sub hello()
VBScript ByVal Parameters: If ByVal is specified, then the arguments are sent as byvalue when the function or procedure is called. Example: option explicit Function fnadd(Byval num1, Byval num2) num1 = 4 num2 = 5 End Function Dim x,y,res x=6 y=4 res= fnadd(x,y) msgbox x msgbox y
VBScript ByRef Parameters If ByRef is specified, then the arguments are sent as a reference when the function or procedure is called. Example: option explicit Function fnadd(byRef num1,ByRef num2) num1 = 4 num2 = 5 End Function Dim x,y,res x=6 y=4 res= fnadd(x,y) msgbox x msgbox y

Array and functions

  • 1.
  • 2.
    What is anarray? • We know very well that a variable is a container to store a value. Sometimes, developers are in a position to hold more than one value in a single variable at a time. When a series of values is stored in a single variable, then it is known as an array variable.
  • 3.
    CREATING ARRAYS • Wecan create arrays several ways, depending on whether they are static or dynamic. • Static arrays - Static arrays stay a fixed size throughout their lifetime— that is, the index size remains constant. Thus, when we create a static array, we must know how many items the array will contain throughout its lifetime. • Dynamic arrays – Suppose if we don't know the number of items or don’t know that the array's index size will change, we need to create a dynamic array. Dynamic arrays don't have a fixed index size. We can increase or decrease the index size at any time.
  • 4.
    Array Declaration andAssigning Values to an Array Example 1: option explicit dim arr(3) arr(0)=1 arr(1)="2" arr(2)="sun" arr(3) = #10/07/2013# msgbox arr(0) msgbox arr(1) msgbox arr(2) msgbox arr(3)
  • 5.
    Example 2: option explicit Dimarr arr=array("5",100,45,464) msgbox arr(0) msgbox arr(1) msgbox arr(2) msgbox arr(3)
  • 6.
    Multi-Dimension Arrays • Arraysare not just limited to single dimension and can have a maximum of 60 dimensions. Two-dimension arrays are the most commonly used ones. Example: Option explicit Dim arr(1,2) //2 rows and 3 columns arr(0,0) = "A" arr(0,1) = "B" arr(0,2) = "C" arr(1,0) = "Dr" arr(1,1) = "E" arr(1,2) = "F" msgbox arr(0,0) msgbox arr(0,1) msgbox arr(0,2) msgbox arr(1,0) msgbox arr(1,1) msgbox arr(1,2)
  • 7.
    ReDim Statement • ReDimStatement is used to declare dynamic-array variables and allocate or reallocate storage space. Example: option explicit dim arr() redim arr(5) arr(0)=1 arr(1)=2 arr(2)=3 arr(3)=4 arr(4)=5 redim preserve arr(10) arr(6)=7 msgbox arr(0) msgbox arr(1) msgbox arr(6) redim preserve arr(4) msgbox arr(0) msgbox arr(1) msgbox arr(2) msgbox arr(3) msgbox arr(4)
  • 8.
    Array Methods LBound Function •The LBound Function returns the smallest subscript of the specified array. Hence, LBound of an array is ZERO. Example: option explicit dim arr(5) arr(0)=1 arr(1)=2 arr(2)=3 arr(3)=4 arr(4)=5 msgbox lbound(arr)
  • 9.
    UBound Function The UBoundFunction returns the Largest subscript of the specified array. Hence, this value corresponds to the size of the array. Example: option explicit dim arr(5) arr(0)=1 arr(1)=2 arr(2)=3 arr(3)=4 arr(4)=5 msgbox ubound(arr)
  • 10.
    Split Function • ASplit Function returns an array that contains a specific number of values split based on a Delimiter. Example: option explicit dim arr,b,c,i arr=split("sun & technology & integrators","&") b=ubound(arr) for i=0 to b msgbox arr(i) next
  • 11.
    Join Function • AFunction, which returns a String that contains a specified number of substrings in an array. This is an exact opposite function of Split Method. Example: option explicit dim arr,b,c arr=array("sun","technology","integrators") b=join(arr) msgbox b c=join(arr,0) msgbox c
  • 12.
    Filter Function A FilterFunction, which returns a zero-based array that contains a subset of a string array based on a specific filter criteria. Example: option explicit Dim MyIndex Dim MyArray (3) MyArray(0) = "Sunday" MyArray(1) = "Monday" MyArray(2) = "Tuesday" MyIndex = Filter(MyArray, "Mon") msgbox myindex(0)
  • 13.
    IsArray Function The IsArrayFunction returns a Boolean value that indicates whether or NOT the specified input variable is an array variable. Example: option explicit dim a,b a=array("Red","Blue","Yellow") b = "12345" msgbox isarray(a) msgbox isarray(b)
  • 14.
    Erase Function The EraseFunction is used to reset the values of fixed size arrays and free the memory of the dynamic arrays. Example: option explicit dim a(2) a(0)="hello" a(1)=22 a(2)=08 msgbox a(0) msgbox a(1) msgbox a(2) erase a msgbox a(0) msgbox a(1) msgbox a(2)
  • 15.
  • 16.
    What is aFunction? A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing same code over and over again. This will enable programmers to divide a big program into a number of small and manageable functions. • Function Definition Before we use a function, we need to define that particular function. The most common way to define a function in VBScript is by using the Function keyword, followed by a unique function name and it may or may not carry a list of parameters and a statement with an End Function keyword
  • 17.
    Example 1: option explicit FunctionHello() msgbox("Hello") End Function call hello()
  • 18.
    Example2: option explicit Function Hello(name,age) msgbox(name & " is " & age & " years old.") End Function call hello("vb",4)
  • 19.
    Example3: option explicit Function sum(number1,number2) sum= number1 + number2 End Function Dim total total = sum(100,9) msgbox total
  • 20.
    Sub-Procedures Sub-Procedures are similarto functions but there are few differences. • Sub-procedures DONOT Return a value while functions may or may not return a value. • Sub-procedures Can be called without call keyword. • Sub-procedures are always enclosed within Sub and End Sub statements.
  • 21.
  • 22.
    VBScript ByVal Parameters: IfByVal is specified, then the arguments are sent as byvalue when the function or procedure is called. Example: option explicit Function fnadd(Byval num1, Byval num2) num1 = 4 num2 = 5 End Function Dim x,y,res x=6 y=4 res= fnadd(x,y) msgbox x msgbox y
  • 23.
    VBScript ByRef Parameters IfByRef is specified, then the arguments are sent as a reference when the function or procedure is called. Example: option explicit Function fnadd(byRef num1,ByRef num2) num1 = 4 num2 = 5 End Function Dim x,y,res x=6 y=4 res= fnadd(x,y) msgbox x msgbox y