1

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

mulla
  • 143
  • 1
  • 11

2 Answers2

5

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.

  • Thanks for the answer. But is there is any logical connectivity that why it is not concatenating in the 3 case – mulla Jul 20 '16 at 09:28
  • I update my answer. The short answer is that VBScript will not cast a number into a string using the `+` operator. –  Jul 20 '16 at 09:46
  • I understand your answer... Let me put it this way.... in the first expression if I use TypeName("1") it will give it as Strings. According to your answer(If One expression is numeric and the other is a string then an Error: type mismatch will be thrown.) with the case 1 it should throw a error. It is adding and I need a logical conclusion on the same. It not something a basic level of question as others think. As by the results we know it is addition. But need to find why it is adding. Let me Update my question with some 3 or 4 cases in the question to depict this... – mulla Jul 20 '16 at 11:20
  • @mulla the example cases you provide are not equivalent. – user692942 Jul 20 '16 at 11:28
  • @Lankymart: But I wanted to know why there is behavior of string as numeric in some parts and not the same in other areas – mulla Jul 20 '16 at 11:36
  • @mulla because for starters everthing in VBScript is typeless all data is `Variant` data type and certain types are inferred. Thing is your doing comparison and then computation which is different. `string = int` is always going to be `False`, `string + int` *(in theory as long as the string contains a valid numeric value will perform a computation, otherwise you'll get `Microsoft VBScript runtime error: Type mismatch: 'y'`)*. – user692942 Jul 20 '16 at 12:14
  • Thanks for the detailed explanation and I am expecting a convincing detail like the one which you have given. Kindly add it as a answer@Lankymart – mulla Jul 20 '16 at 12:26
  • @mulla to be honest I haven't said anything different to what Thomas has said in their answer, may be the one point I'd be picky on is *"If One expression is numeric and the other is a string then an Error: type mismatch will be thrown."* as it's not strictly true, if the string is valid numeric value like `"1"` for example then the computation will still occur without error. – user692942 Jul 20 '16 at 12:37
2

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"]'
Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175