Question Unexpected conversation of string to double +"some string"

titan68mc

New member
Joined
Feb 2, 2012
Messages
1
Programming Experience
10+
I have a question that I hope someone here can answer.

I was attempting to assign a string to a string variable and I accidently put a + in front of the string.

Dim s as String = +”Hello World”

I got the following compile error: "Conversion from string "" to type 'Double' is not valid."

My question is why did the + cast the string to a double.

Thanks
 
+ Operator (Visual Basic) is used to add numbers, so compiler probably parse the expression as 0+(convert string to number).
In general, + performs arithmetic addition when possible, and concatenates only when both expressions are strings.
Turning on Option Strict will prevent things like this to come as a surprise at runtime.
 
Back
Top