VB equivalent of this C# code

P

pvdg42

Jon Paal said:
what is vb equiv. of

++

shown in C# ?
David's answer is correct, but with the ++ (increment) and -- (decrement)
operators, you also need to be aware that the ++ and -- operators each come
in two versions, prefix and postfix, that affect operation in complex
expressions.

int i = 5;
i++; and
++i; do exactly the same thing, add 1 to the current value in the variable
i.

i--; and
--i; also do exactly the same thing, subtract 1 from the current value in i.

So, prefix vs. postfix notation makes no difference in simple expressions.

In an expression like this, however,

Assume i == 5
if (i++ > 5) vs. if(++1 > 5).

there is a difference:
if(i++ > 5) evaluates the Boolean expression based on the *current* value in
i, then adds 1 to i.
The Boolean expression is false.

if(++1 > 5) *first* adds 1 to i, then evaluates the Boolean expression using
the new value in i.
The Boolean expression is true.

As you didn't show the context in which you encountered the ++ in C# code, I
though you should be aware of the difference between prefix and postfix
notation.
 
G

Guest

So when you take it all into consideration, there is no single statement
equivalent to statements containing ++ or --, since VB doesn't allow
assignments within expressions (there is a complex multi-statement equivalent
however). There is only a single statement equivalent for simple "i++" or
"i--" statements.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
C

Cor Ligthert [MVP]

Jon,

I know it only on one place and that is in a "for index".

step 1

Cor
 
B

Branco Medeiros

David said:
So when you take it all into consideration, there is no single statement
equivalent to statements containing ++ or --, since VB doesn't allow
assignments within expressions (there is a complex multi-statement equivalent
however). There is only a single statement equivalent for simple "i++" or
"i--" statements.

Well, if you *really*, *really* want it, nothing but common sense
forbids you from having:

<air-code>
Function PreInc(ByRef Value As Integer) As Integer
Value +=1
Return Value
End Function

Function PostInc(ByRef Value As Integer) As Integer
Dim Result As Integer = Value
Value +=1
Return Result
End Function

Function PreDec(ByRef Value As Integer) As Integer
Value -=1
Return Value
End Function

Function PostDec(ByRef Value As Integer) As Integer
Dim Result As Integer = Value
Value -=1
Return Result
End Function
</air-code>

Regards,

Branco.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Implement a VB interface. 3
vb equivalent 3
VBA currentdb 3
how do I get the version dates in vb 3
VB.Net missing preprocessor directives? 2
#pagrama 3
c++ to vb 2
#Region on VB 4

Top