VB script Tutorial Part 2

Explanation of VBScript for use in HP Unified functional Tool (UFT)
Index -:
1. VB Script Constants
2. VB Scripts Functions and Subroutines
3. VBScript Conditional Statements


VBScript Constants
A constant is a meaningful name that takes the place of a number or string and never changes. The difference between variable and constant is we can change the variable value in run time but for constants it’s not possible.

How to create constant in VB script
Use const keyword to create constant. For example - const str1 = “MicroFocus” that value never change.
VB script provides two types of constants 1. Public and 2. Private
By default all constants are public and you can use it as private using private keyword while declaring.
For example -: 1. Public const str1 “MicroFocus”. 2. Private const str2 = “hpe”.
VBScript Functions and Subroutines
There are two types of procedures available in VB script.
1.    Function Procedure - : It is a series of VBScript statements enclosed by the Function and End Function statements. In Function procedures we can use function name to assign a value. Function Procedure is able to return single value.

Function Fn_addition(a,b)
addition  = a + b
End Function
iReturn = Fn_addition(9,5)
msgbox iReturn
 'Output 14

In above example function returns the single value to iReturn variable.

2.    Sub Procedure -: It is a series of VBScript statements enclosed by the Sub and End Sub statements. Sub Procedure cannot return any value

Sub addition(a,b,iExtraVar)
iExtraVar = a + b
End Sub
iReturn = Addition 9,5,iExtraVar
msgbox iExtraVar
 'Output 14

This example will do the same as what function procedure is doing above. But in sub Procedure we need to use one more parameter to get values from the sub procedure.
Main difference is function return value and sub procedure can’t.

There are two types of parameter for procedures
1.    Call by reference -: In this case, the reference of the original value is passed as the parameter to the function. Therefore, whatever change is done to the parameter inside the function, the same will be reflected in the original parameter also. By default, values are passed by reference in a function. i.e., even if you don’t use byRef keyword, the parameters are passed by reference
For example -:
Dim value
value=5
'Function Call
Fn_CallByRef value
msgbox "Original Value: " & value
'msgbox displays value 7

'Function Definition
Function Fn_CallByRef (ByRef value)
  value = value + 2
  msgbox "New Value: " & value 'msgbox displays
 value 7
End Function


Since the original parameter is passed as reference to the function, both the original and new value has the updated value 7.

2.    Call by value-: In this case passing parameters, only a copy of the original parameters is passed. This means that whatever modifications we make to the parameters inside the function, it doesn’t affect the original parameters.

Dim value
value=5
'Function Call
Fn_CallByValue value
msgbox "Original Value: " & value 'msgbox displays value 5

'Function Definition
Function Fn_CallByValue (ByVal value)
  value = value + 2
  msgbox "New Value: " & value 'msgbox displays value 7
End Function













In the above example you would see that the new value get changed to 7 but it doesn’t get reflected to the original value which still shows the value as 5 only.

Note -: By Default all parameters are ‘ByRef’

VBScript Conditional Statements
There are three types of conditional statements in VbScript.
1.    if ... then ... else statement
Example 1 -:

If value > 5 then
  Msgbox “Greater than 5”
Else
 Msgbox “Less than 5”
End if
Example 2 -:
If value > 5 then
  Msgbox “Greater than 5”
Elseif value = 5 then
 Msgbox “equal to 5”
Else
 Msgbox “Less than 5”
End if

2.    if …. Then statement
Example -:
If value > 5 then Msgbox “Greater than 5”


3.    Select case statement
Example -:
value = InputBox("enter choice")
select Case value
case "Test1"
      msgbox "Test1"
case "Test2"
    msgbox "Test2"
case "Test3"
    msgbox "Test3"
End select



Next post is regarding alternatives of arrays in vbscript 

Comments

Popular posts from this blog

Excel Operation in VBScript

Usage of File system object (FSO) in Vbscript. QTP / UFT

How to set browser language for chrome for localization testing