Overloading of the unary ++ operator in VB.net

V

VB Developer

Overloading of the unary ++ operator in vb.net is not working. It show error:
Operator declaration must be one of: +, -, *, \, /, ^, &, Like, Mod, And,
Or, Xor, Not, <<, >>, =, <>, <, <=, >, >=, CType, IsTrue, IsFalse.

Is there any way can to get rid of this error?

C# is working fine for overloading ++, and do I need to change developing
language to C#? Any suggestion?
 
T

Tom Shelton

Overloading of the unary ++ operator in vb.net is not working. It show error:
Operator declaration must be one of: +, -, *, \, /, ^, &, Like, Mod, And,
Or, Xor, Not, <<, >>, =, <>, <, <=, >, >=, CType, IsTrue, IsFalse.

Is there any way can to get rid of this error?

C# is working fine for overloading ++, and do I need to change developing
language to C#? Any suggestion?

++ is not an operator in VB.NET. As the error says, it must be one of
the listed operators.
 
A

Armin Zingler

VB Developer said:
Overloading of the unary ++ operator in vb.net is not working. It
show error: Operator declaration must be one of: +, -, *, \, /, ^,
&, Like, Mod, And, Or, Xor, Not, <<, >>, =, <>, <, <=, >, >=, CType,
IsTrue, IsFalse.

Is there any way can to get rid of this error?

Yes, don't use "++". "++" is not a valid operator; but not only with
overloading. It does not exist.
C# is working fine for overloading ++, and do I need to change
developing language to C#?

You can't. You are VB Developer.


Armin
 
A

Andrew Morton

Tom said:
++ is not an operator in VB.NET. As the error says, it must be one of
the listed operators.

Or one of +=, *=, &= and so on, which the help/error message seems to miss
out.

Andrew
 
V

VB Developer

+= is not work too.

Code:
Public Shared Operator +=(ByVal bi As Integer) As Integer
....................

End Operator

Give the same error above.

Please check it before post yours.
 
A

Andrew Morton

Patrice said:
Similarly I would say that += etc... doesn't really exists. This is
just a shortcut notation... What if you try just + or whatever fit
your needs ?
You may want to explain what you are trying to do...

/I/'m trying to let the OP know there's no ++ operator in VB.NET... :)

Andrew
 
T

Tom Shelton

+= is not work too.

Code:
Public Shared Operator +=(ByVal bi As Integer) As Integer
...................

End Operator

Give the same error above.

Please check it before post yours.

Actually, looking in the docs, +=, -=, etc are not valid for
overloading, so the error message is perfectly correct. You would have
to overload the +:

Public Shared Operator +(ByVal bi As Integer) As Integer
End Sub

+= and friends are compiler shortcuts - not actual operators.
 
A

Armin Zingler

VB Developer said:
+= is not work too.

Code:
Public Shared Operator +=(ByVal bi As Integer) As Integer
...................

End Operator

Give the same error above.

Please check it before post yours.

See Patrice' reply. The "real" operator is "+". You will be able to use
"dest += value" just like "dest = dest + value"


Armin
 
V

VB Developer

I want to transfer C# code to VB.net code. I know now it is impossible at
some part and except you change original code such as ++ to +1.
 
T

Tom Shelton

I want to transfer C# code to VB.net code. I know now it is impossible at
some part and except you change original code such as ++ to +1.

Yep. VB.NET doesn't support pre/post increment/decrement operators.
The closest you get is:

b += 1

or

b -= 1

And that is just a shortcut for:

b = b + 1

or

b = b -1

One more reson to prefer C# :)
 
H

Herfried K. Wagner [MVP]

VB Developer said:
Overloading of the unary ++ operator in vb.net is not working. It show
error:
Operator declaration must be one of: +, -, *, \, /, ^, &, Like, Mod, And,
Or, Xor, Not, <<, >>, =, <>, <, <=, >, >=, CType, IsTrue, IsFalse.

Is there any way can to get rid of this error?

Overload the '+' operator. You may want to handle the case of
incrementation "by one" differently.
C# is working fine for overloading ++, and do I need to change developing
language to C#? Any suggestion?

Why would you want to overload this operator? Note that this operator is
syntactic sugar, as is '+='. VB does not even support the '++' shorthand
syntax.
 
H

Herfried K. Wagner [MVP]

Tom Shelton said:
Yep. VB.NET doesn't support pre/post increment/decrement operators.
The closest you get is:

b += 1

or

b -= 1

And that is just a shortcut for:

b = b + 1

or

b = b -1

One more reson to prefer C# :)

Well, opinions seem to differ. I don't see any reason for an additional way
to achieve a certain thing which only saves a few keystrokes but requires
additional keystrokes if incrementation/decrementation should be changed
from 1 to another value.
 
T

Tom Shelton

Well, opinions seem to differ. I don't see any reason for an additional way
to achieve a certain thing which only saves a few keystrokes but requires
additional keystrokes if incrementation/decrementation should be changed
from 1 to another value.

it's the pre/post thing that comes in handy sometimes... Not
necessarily the shortcut for += 1.
 

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

Top