-2

I am trying to get this snippet to vb but keep getting errors in the ide

var header = new byte[8];
int index = 0;
var magic_signature = ((uint)header[index++] << 0) | ((uint)header[index++] << 8) | ((uint)header[index++] << 16) | ((uint)header[index++] << 24);

I tried an online coverter and got this (which produces errors)

Dim magic_signature = (CUInt(header(Math.Max(Threading.Interlocked.Increment(index), index - 1))) << 0) Or (CUInt(header(Math.Max(Threading.Interlocked.Increment(index), index - 1))) << 8) Or (CUInt(header(Math.Max(Threading.Interlocked.Increment(index), index - 1))) << 16) Or (CUInt(header(Math.Max(Threading.Interlocked.Increment(index), index - 1))) << 24)

The header variable is filled after declaration. Could someone help me convert the above to vb.net?

EDIT: Populating header via reading a stream

Dim header = New Byte(8) {}
If stream.Read(header, 0, 8) < 8 Then
    Throw New ApplicationException("Incomplete data.")
End If
Nepaluz
  • 669
  • 1
  • 12
  • 27
  • Can you supply the declaration line of `header` in your converted vb.net code please? – Mike_OBrien Aug 07 '17 at 19:02
  • 1
    @Codexer I personally frown on code conversion questions, but it seems people on Meta disagree :) - https://meta.stackexchange.com/questions/54345/please-convert-my-code-to-x-questions . As the OP has already attempted code conversion (I tried it on telerik myself) with no success, I would be inclined to post an answer as the code is concise, and the Op isn't asking us to convert an entire program. – David Wilson Aug 08 '17 at 11:19
  • @Mike_OBrien - I've added the code to declare and populate the header. – Nepaluz Aug 09 '17 at 10:36
  • 1
    That `Math.Max(Threading.Interlocked.Increment(index), index - 1)` silliness for `i++` is broken for when index is 0. That right there is why the translation didn't work. – rskar Aug 09 '17 at 21:08
  • 1
    @rskar agreed, I believe the translator was replacing the `Index++` portion without seeing that the addition had to be done after the evaluation. VB.net does not have an equivalent to the `++` operator so inline functions like this can not be translated by an automated tool reliably. – Mike_OBrien Aug 10 '17 at 05:06

2 Answers2

1

I believe David Wilson is on the right path but as rskar pointed out the Index + 1 portion does not produce the same results as index++ would in c#. The easiest way to get the behavior from c# would be to break the single command into multiple commands and store the results in temp variables, incrementing Index between each step and then evaluating the temp variables at the end. This would result in a lot more code however.

Ex:

Dim temp1 = (CUInt(header(Index)) << 0)
Index += 1
Dim temp2 = (CUInt(header(Index)) << 8)
Index += 1
Dim temp3 = (CUInt(header(Index)) << 16)
Index += 1
Dim magic_signature As UInteger = temp1 Or temp2 Or temp3 Or (CUInt(header(Index)) << 24)

Unfortunately to my knowledge there isn't anything in vb.net that behaves the same as the ++ operator in c#.

Mike_OBrien
  • 1,395
  • 15
  • 34
1

As Mike_OBrien has pointed out, VB doesn't have a built-in ++ - but we can fake one easily enough:

Function PostIncr(ByRef x As Integer) As Integer
    ' PostIncr(i) works like i++
    Dim x0 As Integer = x : x += 1 : Return x0
End Function

After that, conversion is straightforward:

    Dim header(7) As Byte

    ' Make believe this is a stream.Read
    Array.Copy({CByte(1), CByte(2), CByte(3), CByte(4)},
        header, 4)

    Dim index As Integer = 0
    Dim magic_signature = CUInt(header(PostIncr(index))) << 0 Or
        CUInt(header(PostIncr(index))) << 8 Or
        CUInt(header(PostIncr(index))) << 16 Or
        CUInt(header(PostIncr(index))) << 24

And for fun:

Function PreIncr(ByRef x As Integer) As Integer
    ' PreIncr(i) works like ++i
    x += 1 : Return x
End Function
rskar
  • 4,607
  • 25
  • 21