VB script


VBScript has only one data type called a Variant. A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used.

If you use a variable for assigning a numeric value, that variable behaves like a numeric data type. If you assign string value, that variable behaves like a string.

Because of vbscript is having only one data type no need to declare any variable in the script
***********************************************************************************
DIM statement:
***********************************************************************************
if we want We can declare the variables using Dim statement.

Ex:       Dim x
            X=10
***********************************************************************************
Optional explicit :
***********************************************************************************

     It demands the variable declaration,if we are using 'Optional explicit' statement in the code we should declare the variables else it will through the error statement 

 Variable name :
  • Must begin with an alphabetic character.
  • Cannot contain an embedded period.
  • Must not exceed 255 characters.
  • Must be unique in the scope in which it is declared.

***********************************************************************************
Scope of a Variable:
***********************************************************************************
If you declare a variable with in a Function then it is local to that function only. You can access that variable only with in that function. Now It has local scope and is a procedure-level variable.

If you declare a variable with in a Script then it can be used by entire script and can be accessed by all functions. Now It has local scope and is a procedure-level variable. This is a script-level variable, and it has script-level scope.

***********************************************************************************
Array Variables:
***********************************************************************************
a variable containing a single value is a scalar variable. you can create a variable that can contain a series of values using an index number. This is called an array variable. Arrays are useful when you’re storing sets of similar data.You can store any kind of data in an array. The array can hold a combination of data types.

***********************************************************************************
Creating Arrays:
***********************************************************************************
Using Dim Statement we can create an array. we can convert a variable in to an array using array function.

***********************************************************************************
Types of Arrays:
***********************************************************************************
1. Fixed Length Arrays
2. Dynamic Arrays

            Fixed arrays have a specific number of elements in them, whereas dynamic arrays can vary in the number of elements depending on how many are stored in the array.

***********************************************************************************
Creating Fixed Length Arrays:
***********************************************************************************
Dim a(10)

Here ‘a’ is an array and is having 11 elements (Array count starts from 0). Here it’s a fixed size array with size 10.
***********************************************************************************
Creating Dynamic Arrays:
***********************************************************************************
A dynamic array is created in the same way as a fixed array, but you don’t put any bounds in the declaration.

Dim x()
Here ‘x’ is the dynamic array and we can store n number of elements in it. The benefit of a dynamic array is that if you don’t know how large the array will be when you write the code, you can create code that sets or changes the size while the VBScript code is running.

We can store more values in dynamic array by redeclaring the array using Redim statement.
***********************************************************************************
ReDim Statement:
***********************************************************************************
               Using Redim we can redeclare an array size. ReDim tells VBScript to “re-dimension” the array for how many elements you specify. If you use redim statement in your script it will clear all existing data which is stored in that array and declares that array as fresh array.

***********************************************************************************
Redim with Preserve Keyword:
***********************************************************************************
           When your working redim it will clear all the existing data in an array. The preserve keyword is useful to overcome this problem. Preserve keyword will preserve the existing data and resize the array with the specified size.

Ex: Redim preserve a(20)

***********************************************************************************
Using Fixed arrays:
***********************************************************************************
Dim x(2)

x(0)=”learn”
x(1)=”Qtp”
x(2)=”Concepts”

for i=lbound(x) to ubound (x)
msgbox x(i)
Next

Here we cann’t store more than 3 elements. Because this is a fixed length array..
***********************************************************************************
Using Dynamic Arrays:
***********************************************************************************
Dim x()
Redim preserve x(2)
x(0)=”learn”
x(1)=”qtp”
x(2)=”concepts”
Redim preserve x(3)
x(3)=123

Here ‘x’ is a dynamic array and by redeclaring x it can able to store more values into it.
***********************************************************************************
Converting a variable into an array:
***********************************************************************************
                We can convert a variable in to array variable using array function.

Example
Dim v
v=array(“I”,”Love”,”India”)
for i=lbound(v) to ubound (v)
     msgbox v(i)
Next

Lbound := Using Lbound we can find the lower size of the array
Ubound := Using Ubound we can find the Upper size of the array
Note := if no elements present in the array Lbound will return "-1"
***********************************************************************************
Creating constants:
***********************************************************************************
const str=”QTP”
here str is a constant and the value will never change.

We have public and Private constants. By default all are public. If you want specify the type then

Public const str=”QTP”
or
Private const str=”QTP”

***********************************************************************************
VB Script Procedures:
***********************************************************************************
There are two types of procedures

1. Function Procedure
2. Sub Procedure
***********************************************************************************
Function Procedure:
***********************************************************************************
A Function procedure is a series of VBScript statements enclosed by the Function and End Function statements. Function Procedure can able to return the value.

Example:

Function demo_add(a,b)
                 demo_add=a+b
End Function
oVal=demo_add(2,3)
msgbox oVal    ‘Returns 5

             In this example demo_add function returns a value to oVal. In Function procedures we can use function name to assign a value.

***********************************************************************************
Sub Procedure:
***********************************************************************************
                A Sub procedure is a series of VBScript statements enclosed by the Sub and End Sub statements. Sub Procedure cannot return any value. But in sub Procedure we need to use one more parameter to get values from the sub procedure.

***********************************************************************************
Types of arguments in procedures:
***********************************************************************************

1. ByVal
2. ByRef

ByVal:
        Indicates that the argument is passed by value.
ByRef :
      Indicates that the argument is passed by reference.

By default all arguments are ‘ByRef’.

           In simple words ByRef Means the value which is assigned to the variable with in the function is permanent and we can use that value out side of that function also.

          ByVal means the value which is assigned to the variable with in the function is temporary and we can use that value only with in that function.

***********************************************************************************
Returning multiple values from a Function:
***********************************************************************************
Usually Function procedure will return only one value. But by using arrays concept we can return multiple values from a function.


***********************************************************************************

No comments:

Post a Comment