Saturday 29 March 2014

Functions in VBScript

Functions are used to perform the specific task.Functions are used to increase the reusability of the code. Functions are similar to the procedures except one difference that functions can return value.

Simple example of the function


Suppose you want to find the sum of 2 numbers. Without functions, you will write below code.

a = 10
b = 20
c = a+ b
Msgbox c

To add another 2 different numbers, you will use below code

a = 33
b = 65
c = a+ b
Msgbox c

In above examples, we have to add 2 numbers. So the operation is same (Repeating) So we can write the function which will take 2 parameters as shown below.

function sum(byref a, byref b)

    sum = cint(a) + cint(b)

end function

Calling functions in VBScript

To call the procedures we can use below statements.

c = sum(10,20)
msgbox c
c =  sum(33,65)
msgbox c

Thus we can call functions many times in the code. This reduces the lines of code  as well as maintainability of the code.

Passing arguments to the function

We can pass the arguments to the function by 2 ways.
  1. Pass by reference 
  2. Pass by value
Pass by reference

By default, values are passed to the function by reference. When we pass the values by reference the changes made to the variables in the called function are reflected in the calling function/procedure.

a = 10
c = findsqr(a)
msgbox a  'prints 100
msgbox c  'prints 100

function findsqr(byref a)

  a = a*a

  findsqr = a

end function


Pass by value

When we pass the values by value, the changes made to the variables in the called function are not reflected in the calling procedure or function.

a = 10
c = findsqr(a)
msgbox a  'prints 10
msgbox c  'prints 100

function findsqr(byval a)

  a = a*a

 findsqr = a

end function

Other built-in functions in VBScript

  1. msgbox
  2. inputbox
  3. eval 
  4. execute
msgbox function is used to show message to the user.
inputbox function is used to read the value from the user.

a = inputbox("Enter the number")
msgbox a 'prints 33

Inputbox function in vbscript
Eval and execute functions are used to execute any valid vbscript expression. Difference between 2 functions is that the operator = is handled in different ways.

Eval always uses = as a comparison operator.
Execute always uses = as a assignment operator.

Example -
a=20
msgbox eval("a=10")  'will print false
msgbox a

a=20
execute("a=10")
msgbox a    'will print 10






Sub Procedures in VBScript

Sub procedures are used to perform the specific task. Sub procedures are used to increase the reusability of the code.

Simple example of the procedure


Suppose you want to find the sum of 2 numbers. Without procedures, you will write below code.

a = 10
b = 20
c = a+ b
Msgbox c

To add another 2 different numbers, you will use below code

a = 33
b = 65
c = a+ b
Msgbox c

In above examples, we have to add 2 numbers. So the operation is same (Repeating) So we can write the procedure which will take 2 parameters as shown below.

sub sum(byref a, byref b)

    msgbox cint(a) + cint(b)

end sub

Calling procedures in VBScript

To call the procedures we can use below statements.

Call sum(10,20)
Call sum(33,65)

Thus we can call procedures many times in the code. This reduces the lines of code as well as maintainability of the code.

Passing arguments to the procedure

We can pass the arguments to the procedure by 2 ways.
  1. Pass by reference 
  2. Pass by value

Pass by reference

By default, values are passed to the procedure by reference. When we pass the values by reference the changes made to the variables in the called procedure are reflected in the calling procedure.

a = 10
call findsqr(a)
msgbox a  'prints 100

sub findsqr(byref a)

  a = a*a

  msgbox a   'prints 100

end sub


Pass by value

When we pass the values by value, the changes made to the variables in the called procedure are not reflected in the calling procedure.

a = 10
call findsqr(a)
msgbox a  'prints 10

sub findsqr(byval a)

  a = a*a

  msgbox a   'prints 100

end sub




Basic Syntax in VBScript

Basic VBScript Syntax
Remember below points about VBScript Syntax.
  1. VBScript is loosely typed language that means you do not need to declare the variables with data type
  2. VBScript comments start with symbol '. You can also comment using rem keyword.
  3. You do not need to put semicolon at the end of the statement like C,C++ and JAVA
VBScript also provides below functions that can be used to find out more information about the variables.

  1. IsArray - This function can be used to check if the given variable is an array or not
  2. IsDate - This function can be used to check if the given variable is a valid date or not
  3. IsEmpty - This function can be used to check if the given variable is empty or not
  4. IsNull - This function can be used to check if the given variable is a null or not
  5. IsNumeric - This function can be used to check if the given variable is valid number or not
  6. IsObject - This function can be used to check if the given variable is valid object or not
  7. TypeName - This function returns the data type of the given variable.
Examples - 


dim a(4)
dim b
b=10

Msgbox  "a is array? -> " & IsArray(a)     'prints true

Msgbox  "b is a date? -> " & IsDate(a)  'prints false

Msgbox  "b is empty? -> " & IsEmpty(b)  'prints false

Msgbox  "b is null? -> " & IsNull(b)   'prints false

Msgbox  "b is numeric? -> " & IsNumeric(b)   'prints true

Msgbox  "b is object? -> " & IsObject(b)   'prints false

Msgbox  "TypeName of b -> " & TypeName(b)   'prints integer

Friday 28 March 2014

Vbscript Tutorial for Beginners

We are going to cover all major topics on vbscript in this tutorial for beginners.

  1. VBScript - Intro and applications
  2. Variables, Data Types (Conversions) and Operators, comments, Set in VBScript and  IsArray, IsDate, IsEmpty , IsNull , IsNumeric , IsObject ,TypeName ,VarType
  3. Arrays in VBScript - types of array
  4. Control Statement - Conditional  and  looping 
  5. Procedures and Functions - eval, execute, inputbox, msgbox and difference between function and sub
  6. Date and Time
  7. Strings
  8. Maths Functions
  9. Classes in VBScript - Constructors
  10. Regular Expressions in Vbscript 
  11. Dictionary in VBScript
  12. File System in VBScript
  13. Error Handling
  14. WSH
  15. WMI and Win32 classes

Classes in VBScript

Here is an example that shows how we can create a class in QTP. Once we define the class, we can create its objects and then access its method and properties.


'declare the class book.

Class Book

dim bn,bp
'class has 2 variable members bn and bp.

public Property Get bookname()
    ' Get bookname property gets the book name of the object of Book
  bookname = bn
End Property

public Property Let  bookname(x)
  bn = x
   ' Let  bookname property assigns value to the book name of the object of Book
End Property


public Property Get price()
price = bp
End Property

public Property Let  price(x)
bp = x
End Property


function  discountedPrice()
print bp-20
'We can have functions and procedures inside class to process memeber variables
End function


Private Sub Class_Initialize   ' Setup Initialize event.

MsgBox("Object of Book Class created")

End Sub

Private Sub Class_Terminate   ' Setup Terminate event.

MsgBox("Object of Book Class destroyed")

End Sub


End Class

Set b1 = new  Book
'createing the object b2 of the class Book.

b1.bookname = "QTP Tutorials"
b1.price = 220
'assigning value to the object b1

print b1.bookname()
'getting the value of the property bookname.

'accessing the function in class
b1.discountedPrice()

Set b1 = nothing


QTP does support object oriented programming to some extent. We can create constructors and destructors in vbscript classes using class_initialize and class_terminate methods. class_initialize method acts like constructor function which gets called automatically when we create an object of the class. This is how we can create and use the classes in QTP.

Error handling in VBScript

VBScript provides below statements and keywords to handle errors.
  1. on error resume next
  2. Err object - err.description, err.number

Sample code to handle the error is given below

If err.number <> 0 Then
print err.description
else
print "there  was no error in above statement"
end if


Whenever any error occurs in the script, We get the message window with detailed description of the error.

But when we are executing the scripts, we do not want this message box to appear to come as this will halt the execution of the script.

To prevent message box from appearing, we use below statement above the block of code.

On error resume next
----------
----------
more statements

With On error resume next in placeVBScript runs even though error exists in the code. We can capture those errors using Err object as stated earlier.

When you are debugging the scripts, you should not use On error resume next statement as it will suppress the errors and you will not be able to figure out the issue in your script.

Total Pageviews