Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No increment operator in VB.net

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)

like image 226
V4Vendetta Avatar asked Jun 14 '11 05:06

V4Vendetta


People also ask

What is the increment in VB?

The ++ and the –- are the increment and decrement operators. Increment(++) operator. The increment operator increases its operand by one.

What does the ++ operator do on an array?

The ++ and -- operators act on scalar values, incrementing or decrementing them. An array (not a pointer) is an aggregate of values (an ordered list of values stored contiguously in memory) so it cannot be incremented or decremented.

What is increment operator with example?

Increment operator can be demonstrated by an example: #include <stdio.h> int main() { int c = 2; printf("%d\n", c++); // this statement displays 2, then c is incremented by 1 to 3. printf("%d", ++c); // this statement increments c by 1, then c is displayed. return 0; }

What does != Mean in VB?

<> in VB.NET means "not equal to". It can be used with the normal oprands as well as in comparision with the items when compared with the datas fetched with the data reader (from database). Follow this answer to receive notifications.


1 Answers

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) through C++, Java and C#.

The VB lineage comes from the original BASIC from Dartmouth (and, earlier, Fortran) and is a different beast altogether.

In other words, what started as the venerable BASIC:

LET I = I + 1 

has probably been hacked and destroyed enough :-)

As per Eric's post, i++; is indeed just an expression, one that yields i with the side effect that i is incremented after the event (similar to the non-side-effect expression i;).

That's because C allows these naked expressions, even things like 42; which doesn't really do much but is perfectly valid. In other words, the following is a complete C program:

int main (void) { 1; 2; 3; 4; 5; 6; 7; 8; 9; return 0; } 

All those expressions are valid but useless (except the 0 at the end of course).

In BASIC, this was not really done, because BASIC consisted of statements (things that did something). That's why i += 1 (a statement incrementing i) is considered okay, but i++ (an expression doing nothing which just happens to have a side effect which increments i) 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:

ADD 1 TO DD_WS_I. 
like image 100
paxdiablo Avatar answered Sep 22 '22 05:09

paxdiablo