When creating messages in C# I've done something similar to this.
ByteMessage[i] = "A"
ByteMessage[++i] = "B"
ByteMessage[++i] = "C"
......................
I would like to accomplish this in VB.NET, except VB.NET does not support the ++ or -- operators. I've tried things like
ByteMessage(i += 1) = "A"
ByteMessage(i += 1) = "B"
ByteMessage(i += 1) = "C"
......................
But this doesn't seem to work. The MATH library didn't seem to have anything in it either for use.
Is there any clean solution for this in VB.NET like there is for C#?
There is nothing inherent in .Net that supports pre/post increment and return functionality. The C# compiler supports its by emitting the required IL statements when it compiles the code. While VB.Net language developers did not deem this feature necessary, there is nothing that prevents you from using Extension Methods to add this functionality.
The only limitation is that you need to use valid method name (for example IncrPre or IncrPost) for the extension method and use method notation instead of the ++i or i++ notation.
Public Module Int32Extensions
<Extension()>
Public Function IncrPost(ByRef i As Int32) As Int32
Dim ret As Int32 = i
i += 1
Return ret
End Function
<Extension()>
Public Function IncrPre(ByRef i As Int32) As Int32
i += 1
Return i
End Function
End Module
Example usage:
Dim i As Int32 = 0
Dim ByteMessage As String() = New String(0 To 4) {}
ByteMessage(i) = "A"
ByteMessage(i.IncrPre) = "B"
ByteMessage(i.IncrPre) = "C"
i = 3
ByteMessage(i.IncrPost) = "D"
ByteMessage(i) = "E"
yields:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With