Home > Posts > Gotcha: no prefix increment/decrement VB.net operators, but get parsed

Gotcha: no prefix increment/decrement VB.net operators, but get parsed

In C/C++ and C# one has useful prefix (++index) and postfix (index++) increment operators and corresponding decrement ones.

Although they have a single operant, they’re not working like functions as single + or – prefix would do, but instead they cause side-effects – they edit the variable passed to them. The prefix increment operator first increases a variable’s value, then returns it as result, while the postfix one first returns a variable’s value, then increases it.

Visual Basic.net doesn’t have such operators (and they don’t seem to plan to add such from their response on related feedback at Microsoft Connect.

However, if you type-in

Dim x = ++y

the compiler won’t complain. In fact, it won’t complain even if you type

Dim x = +++++y

The reason is because it thinks this last crazy-looking expression is:

Dim x = +(+(+(+(+y))))

Similarly, it accepts expressions like

Dim x = –y

and

Dim x = —–y

So a programmer coming from C# or converting C# code to VB.net has to be extra careful when converting ++index expressions to use their own implementation as a function that takes a ByRef parameter

  1. No comments yet.
  1. No trackbacks yet.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.