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 –)
whereas i was able it do something like cnt +=1
I researched a bit and came across Eric’s post on the same, but wasn’t really able to understand fully on it.
He mentions of In VB, a STATEMENT cannot be just an EXPRESSION. not sure how that really fits in.
I hope someone here would be able to explain why this doesn’t work in the same way as it does in C#.
(Hope this will also hold true as in why we have == in C# for comparison)
I would say that the language designers simply thought that BASIC was a better baseline than C, when designing Visual BASIC. You can follow the lineage of
C(and, earlier,BCPL) throughC++,JavaandC#.The
VBlineage comes from the originalBASICfrom Dartmouth (and, earlier,Fortran) and is a different beast altogether.In other words, what started as the venerable
BASIC:has probably been hacked and destroyed enough 🙂
As per Eric’s post,
i++;is indeed just an expression, one that yieldsiwith the side effect thatiis incremented after the event (similar to the non-side-effect expressioni;).That’s because
Callows these naked expressions, even things like42;which doesn’t really do much but is perfectly valid. In other words, the following is a completeCprogram:All those expressions are valid but useless (except the
0at the end of course).In
BASIC, this was not really done, becauseBASICconsisted of statements (things that did something). That’s whyi += 1(a statement incrementingi) is considered okay, buti++(an expression doing nothing which just happens to have a side effect which incrementsi) isn’t. You could argue that it’s just semantic hair-splitting but that’s just the way it is.You should be thankful for small mercies, at least you’re not having to deal with COBOL: