mulla
mulla

Reputation: 143

Usage of + operator in differents situations in vbscript

What is the Value of the below in vbscript

1)x=1+"1" 2)x="1"+"1" 3)x=1+"mulla" Note:In the all above three cases I am using first variable as either string or integer and second on as always as string.

Case 1:Acting as a numeric and auto conversion to numeric during operation

enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
msgbox x+y Rem value is 2
msgbox x*y Rem value is 1

Case 2:Acting as a String and no conversion to numeric during operation it fails

enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
if y= x then
    msgbox "pass"
else
    msgbox "fail"
end if

Case 3:Acting as a String and explicit conversion to numeric during operation it passes

enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
if Cint(y) = x then
    msgbox "pass"
else
    msgbox "fail"
end if

I need a logic reason for the different behaviors. but in other language it is straight forward and will work as expected

Upvotes: 1

Views: 192

Answers (2)

user692942
user692942

Reputation: 16681

I agree with most of what @thomas-inzina has said but the OP has asked for a more detailed explanation so here goes.

As @thomas-inzina point's out using + is dangerous when working with strings and can lead to ambiguity depending on how you combine different values.

VBScript is a scripting language and unlike it's big brothers (VB, VBA and VB.Net) it's typeless only (some debate about VB and VBA also being able to be typeless but that's another topic entirely) which means it uses one data type known as Variant. Variant can infer other data types such as Integer, String, DateTime etc which is where the ambiguity can arise.

This means that you can get some unexpected behaviour when using + instead of & as + is not only a concatenation operator when being used with strings but also a addition operator when working with numeric data types.

Dim x: x = 1
Dim y: y = "1"

WScript.Echo x + y

Output:

2
Dim x: x = "1"
Dim y: y = "1"

WScript.Echo x + y

Output:

11
Dim x: x = 1
Dim y: y = 1

WScript.Echo x + y

Output:

2
Dim x: x = 1
Dim y: y = "a"

WScript.Echo x + y

Output:

Microsoft VBScript runtime error (4, 5) : Type mismatch: '[string: "a"]'

Upvotes: 2

user6432984
user6432984

Reputation:

Reference: Addition Operator (+) (VBScript)

Although you can also use the + operator to concatenate two character strings, you should use the & operator for concatenation to eliminate ambiguity. When you use the + operator, you may not be able to determine whether addition or string concatenation will occur. The type of the expressions determines the behavior of the + operator in the following way:

If Both expressions are numeric then the result is the addition of both numbers.

If Both expressions are strings then the result is the concatenation of both strings.

If One expression is numeric and the other is a string then an Error: type mismatch will be thrown.

When working with mixed data types it is best to cast your variables into a common data type using a Type Conversion Function.

Upvotes: 5

Related Questions