2
Sub V(N As Integer)
    Console.WriteLine(N)
End Sub

Sub Main()
    Dim N = 0
    For I As Integer = 1 To 5
        V(++N)
    Next
End Sub

VB.Net does not have preincrement operator, ++N wouldn't work outside of a function argument. Why does this code compile?

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
user6144226
  • 613
  • 1
  • 8
  • 15

1 Answers1

1

Unlike C#, there is no increment operator in Vb.Net, the +/- symbols are treated as positive/negative arithmetic signs (or sum/rest if wrote between blankspaces, or if wrote before an assignation symbol like +=/-=), however, you can acchieve what you want in a similar way using the System.threading.Interlocked.Increment function.

Imports System.Threading.Interlocked

Module Module1

    Sub Main()
        Dim value As Integer

        For count As Integer = 1 To 5
            Module1.Method(Increment(value))
        Next count 
    End Sub

    Sub Method(ByVal value As Integer)
        Console.WriteLine(value)
    End Sub

End Module
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Unlike the - operator ("flip" the sign of a number) a + operator seems kind of pointless? I guess a question of why an operatator that doesn't really do anything exists is considered offtopic? – user6144226 Apr 01 '16 at 21:26