No increment operator in VB.net

后端 未结 5 1838
春和景丽
春和景丽 2020-12-15 03:41

I am fairly new to vb.net and came across this issue while converting a for loop in C# to VB.net I realized that the increment operators are not available in vb.net (++ and

5条回答
  •  死守一世寂寞
    2020-12-15 04:00

    As @paxdiablo said, in VB (or rather, in its ancestor BASIC), everything used to be a statement. And in fact, every statement was introduced by a keyword.

    So to assign a variable we had

    LET x = x + 1
    

    and to call a method, we had

    CALL SomeMethod
    

    In VB, the LET and CALL were finally dropped (except in one special circumstance) because it’s completely redundant and doesn’t add clarity. But the underlying lexical grammar of VB didn’t change all that much: each statement still has to be a statement. i++ isn’t a statement in VB, since it lacks either a function call or an assignment.

    There was an argument in the first version of VB.NET whether to introduce pre- and post-increment operators like in C#. It was decided not to do this, for a fairly simple reason: using side-effects in expressions isn’t recommended anyway. It usually lets clarity suffer. So even in C# legitimate uses of i++ in an expression are very rare, and legitimate uses of ++i are rarer still (though I won’t deny that in some cases it adds clarity).

    In most cases you can use i += 1 just fine and this perfectly well expresses the intent.

    Notice that in C++, the situation is fundamentally different because here (but not in C#!) i++ actually has a different semantic than i += 1 due to operator overloading (in C# we also have operator overloading but ++ cannot be overloaded).

提交回复
热议问题