Reputation: 748
I'm fairly new to Visual Basic (my background is mostly C#) so I only recently found out that ++i
is not the same thing as i+=1
in VB.NET. However, my VB.NET code still compiles when I pass ++i
as a parameter, even though it doesn't increment the variable:
++i 'doesn't compile
i++ 'doesn't compile
Foobar(i++) 'doesn't compile
Foobar(++i) 'compiles, but doesn't increment i
The fact that the last statement above doesn't cause an error leads me to wonder if ++
actually does mean something in VB.NET, just not what I thought it meant at first. If it doesn't, is there some other reason it isn't causing an error?
Upvotes: 2
Views: 262
Reputation: 29092
It is just the unary version of the +
operator (see docs) applied twice.
Foobar(++i)
Foobar(+(+i))
Foobar(+(+(i)))
' These are all the same
For numerical values, the unary +
(i.e. the +
operator without second operand) does nothing:
If expression2 is absent, the + operator is the unary identity operator for the unchanged value of an expression.
However, it is not entirely clear from the docs what it will do to non-numeric values. The docs explain various cases with two operands, which all don't seem to apply here.
There is even one sentence that sounds like it could be applied, but it does not do what it says if used with unary +
:
If either
Object
expression evaluates toNothing
orDBNull
, the+
operator treats it as aString
with a value of""
.
So you would expect that +Nothing
gives ""
as well, but it gives 0
instead. In fact, it appears that the unary +
converts non-numerical types to Double
, including strings for which +
would otherwise mean concatenation (for example +"1.234"
gives 1.234
, and +"Hello"
gives an error that this string cannot be converted to Double
- and with Option Strict On
, you can't convert any string implicitly anyway). It seems to behave more like a binary +
with 0.0
as first operand.
You can also overload the unary +
separately from the binary +
and give it a different meaning entirely*. (Or do the opposite - make it do nothing even on a non-numeric type, such as what TimeSpan
does - it returns the original timespan again when unary +
is applied on it, and not a Double
.)
*: Which probably is not such a good idea though. When overloading an operator, the meaning of it should always be intuitive.
Upvotes: 3
Reputation: 54487
There is no ++
operator in VB. The +
unary operator is just like the -
unary operator applied to a number. Numbers are positive by default but, just as you can explicitly make a number negative by prefixing it with a -
operator, you can make it explicitly positive by prefixing it with a +
operator. You can use as many +
operators as you like. Similarly, you can use as many -
operators as you like. The difference is that +
operators don't change the value where -
operators do.
In C-based languages, assignments actually return a value where they don't in VB. In C#, you can do this:
i += 1
and it will get the value of i
, add 1 to it, assign the result back to i
and then return that result, so you can use that expression where a value is expected. In VB, it does all the same things up to the assignment but it does not return a value, so you cannot use that expression where a value is expected.
In C-based languages, where you place the ++
operator makes a difference. The expression:
++i
increments i
and returns the final value, whereas this expression:
i++
increments i
and returns the original value. That's why some argue that the C++ language should actually be named ++C.
Upvotes: 1