I sometimes wonder why Microsoft decided to make 2 teams, when they launched the .NET platform. The small differences between C# and VB.NET sometimes keeps me up all night – wondering why’o’why didn’t I start this project as a C#-project.
Postfix, prefix operators in VB.NET
In C# the ++ var, and var ++ operators a quit nigty operators. They increments their operand by 1, either pre (++ var) or post (++).
But somehow the VB.NET team neglected to support this (and other operators). Why is still a big question to me? But therefore I am sharing my knowledge, but supplying a VB.NET implementation.
”’ <summary></summary>
”’ <remarks>increment operator after (same as C# var++)</remarks>
Public Shared Function OprIncrAft(ByRef operand As Integer) As Integer
OprIncrAft = operand
operand += 1
End Function
”’ <summary></summary>
”’ <remarks>increment operator before (same as C# ++var)</remarks>
Public Shared Function OprIncrBef(ByRef operand As Integer) As Integer
operand += 1
Return operand
End Function
What are they? These operators? Let us take a example.
Dim MyVar As Integer = 1
Console.WriteLine OprIncrBef(MyVar) ‘ Returns 2
MyVar = 1
Console.WriteLine OprIncrAft(MyVar) ‘ Returns 1
Console.WriteLine OprIncrAft(MyVar) ‘ Returns 2
You can easily expand the model, to support incrementials by a certain value.