Reputation: 9
Consider this code:
Dim a As Integer = 4
Dim c As Integer = 3
Console.WriteLine(a.ToString + c)
Console.ReadLine()
it should return 43 as a is being converted to string but still return 7
Upvotes: 1
Views: 53
Reputation: 612993
Because you need to convert both operands to string, and/or use the string concatenation operator &
.
As it stands, you are evaluating this expression:
"4" + 3
and VB decides to convert the first operand to be an integer to match the second operand. VB will only perform string concatenation with +
if both operands are strings. It prefers arithmetic with +
.
Some useful links:
As you can see from these links, the setting of Option Strict
plays a role. You clearly have it set to Off
but frankly setting it to On
would be prudent.
Personally I'd write it like this
a.ToString & c.ToString
The bottom line is that if you know you want to concatenate strings, it is always clearer to use the dedicated string concatenation operator &
.
Upvotes: 2