VBScript Operators

0
73
Spread the love
  • 22
  • 23
  • 16
  • 12
  •  
  •  
  •  
  •  
  •  
  •  
    73
    Shares

VBScript Operator
VBScript Operator

What is an Operator in VBScript?

An Operator is a keyword that operates on two operands to generate an output.Operators are used for performing mathematical, comparison and logical operations.These tutorial holds good for Operators in QTP as well.

VBScript has a full range of operators, including

  • Assignment operator
  • Arithmetic operators
  • Comparison operators
  • Concatenation operators
  • Logical operators
  • Bitwise Operators

Assignment Operator:

Assignment operator is used to assign a value to a/series of  variable.

Sign Name syntax Description
= Assignment Operator Variable=value Assigns the right hand side value or expression to right hand side
Exp=x=5

Arithmetic Operator:

Arithmetic operators supports arithmetic operations on numeric values.We can use them with assignment operator.

Sign Name Syntax Description
^ Exponentiatial 2^5 this operator raises a given number to the power of a given exponent.

The number can be negative if the exponent is an integer. The exponent is evaluated from left to right.

Incase there are more than one exponent in a single expression, the result will be “Null” if the number or exponent is “Null”.

* Multiplication 2*5 Multiplies two numbers/variables.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

/ Floating-point division 5/4 returns a floating point result when this operator is applied.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

Division 3/2 returns the integer from the division operation.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

Mod Modulo 3 mod 2 Returns the remainder when dividing two given numbers/variables.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

+ Addition 3+2 returns the result after addition of two given number/variable.

The result is an integer if both expressions ate Empty. In case one expression is empty then it has no impact on other expression. The other expression will be unchanged.

If one or both the expressions are Null then the result will be null.

 

Subtraction 3-2 returns the result after subtraction of two given number/variable.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

Note:The numeric expressions are rounded to byte/integer/long subtype before integer division takes place.Subtraction supports the unary Subtraction as well
Example-


 -(5-3)=-2
 

For addition if both the variables are of type numeric then they will add. If one of them are String,it will throw VBRuntime error.If both are type String they will concatenate to Single String.
Like-


"Hi"+5//generates run time error
"1"+"5"="15"
"Hi"+" "+"Good Moring"="Hi Good Morning" 
Unary Negation:

Unary negation is part of arithmetic operator.The unary negation operator(-)  indicates the negative value of the numeric expression. It is applicable for variable or value.

Example:

Option Explicit
Dim a
a=-45
msgbox a 
//it represents the negative number

 

Comparison operators:

Comparison operators enables the coder to test two expressions. It compares if the two expressions are True or False.Hence the datatype of Comparison operator is always boolean.

Sign Name Syntax Description
= Equal to variable1=variable2 returns true if variable1=variable2
else returns false
<> Not equal to variable1<>variable2 returns true if variable1<>variable2
else returns false
< less than variable1<variable2 returns true if variable1<variable2
else returns false
> greater than variable1>variable2 returns true if variable1>variable2
else returns false
>= Greater than or equal to variable1>=variable2 returns true if variable1>=variable2
else returns false
<= Less than or equal to variable1<=variable2 returns true if variable1<=variable2
else returns false
Is Compare objects Object1 Is Object2 returns true if Object1 Is Object2
else returns false
Is Not Compare objects Object1 Is Not Object2 returns true if Object1 Is Not Object2
else returns false

In last two cases VbScript Engine checks if both the objects refers to the same memory locations.We need to use Nothing keyword with Is or Is Not operator to check the location or object reference’s validity. It will return true if the object has been destroyed before this operation.

Example:

Set a =CreateObject("Scripting.Dictionary")
Set b =CreateObject("Scripting.Dictionary")
Set c =CreateObject("Scripting.Dictionary")

Set a=c
Set b=c

msgbox a Is b

The output is true.

Note: Is(compare object operator) does not check the value in order to test if they are same kind of object. Rather it verifies if two objects refers to the same object.(Or two objects refers to the same memory location)

Concatenation operators:

Sign Name Syntax Description
& Ampersand Variable1 & variable 2 it concatenates two given expressions.

In case an expression is empty , it will be treated as space (” “). The result is Null if both the expressions are Null.

Incase one expression is null, then it will also be treated as zero length string(“”)

+ Plus Variable1 + variable 2 it concatenates two given expressions if they are string.

The & operator converts the variable to String Subtype before concatenation that means when we use ‘&’ and concatenate a variable with string then an implicit conversion takes place from the result subtype to String subtype. Then it gets concatenated.

Be careful with & and + operator in VBscript as well as UFT.

+ operator concatenates when both the variables are String.


Dim a,b,c,d
a="Hello"
b="Good Morning"
c=a + b
msgbox c
d=a & b
msgbox d
 

However for other datatypes + has a different side effect.


Dim a
Dim b
a=cStr(10)
b=CLng(50)
msgbox a + b
 

Here the output is different than we expected. The bottom line is that we need to make sure before we apply + operator. In case of + operator the operands are of type String.Other wise for concatenation we need to use &. It is safe.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 22
  • 23
  • 16
  • 12
  •  
  •  
  •  
  •  
  •  
  •  
    73
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here