11

I would like to know the VB.NET equivalent of the following C# code:

    unsafe
    {
        byte* pStart = (byte*)(void*)writeableBitmap.BackBuffer;
        int nL = writeableBitmap.BackBufferStride;

        for (int r = 0; r < 16; r++)
        {
            for (int g = 0; g < 16; g++)
            {
                for (int b = 0; b < 16; b++)
                {
                    int nX = (g % 4) * 16 + b;                            
                    int nY = r*4 + (int)(g/4);

                    *(pStart + nY*nL + nX*3 + 0) = (byte)(b * 17);
                    *(pStart + nY*nL + nX*3 + 1) = (byte)(g * 17);
                    *(pStart + nY*nL + nX*3 + 2) = (byte)(r * 17);
                 }
            }
        }
   }
2

3 Answers 3

31

Looks like it's not possible.

From this post

VB.NET is more restrictive than C# in this respect. It does not permit the use of unsafe code under any circumstances.

3

VB.NET does not allow use unsafe code, but you can do your code in safe managed:

Dim pStart As IntPtr = AddressOf (writeableBitmap.BackBuffer())
Dim nL As Integer = writeableBitmap.BackBufferStride

For r As Integer = 0 To 15
    For g As Integer = 0 To 15
        For b As Integer = 0 To 15
            Dim nX As Integer = (g Mod 4) * 16 + b
            Dim nY As Integer = r * 4 + CInt(g \ 4)

            Marshal.WriteInt32((pStart + nY * nL + nX * 3 + 0),(b * 17))
            Marshal.WriteInt32((pStart + nY * nL + nX * 3 + 1),(g * 17))
            Marshal.WriteInt32((pStart + nY * nL + nX * 3 + 2),(r * 17))
        Next
    Next
Next
10
  • 3
    This is not the same. Unsafe code allows the use of pointers, this managed code uses references. References are significantly slower than pointers.
    – Nick
    May 14, 2014 at 13:22
  • 5
    That's like saying a bubble sort is the same as a quick sort just because your eye can't see any difference with a small set of items.
    – Nick
    May 16, 2014 at 12:49
  • 5
    This isn't much of a conversation. References are significantly slower than pointers. The question was how to use unsafe code in vb.net and your answer answer is to use managed code. Managed code is very different than unsafe code and is MUCH slower than "just" double the time. I don't know why you refuse to accept the fact. You can deny it all you like but that won't change reality. And your comments indicate that you have no experience at all writing scalable software that needs to perform well under load.
    – Nick
    May 18, 2014 at 20:28
  • 3
    VB.Net does not allow unsafe code, and the managed VB.Net code in your answer is not the same as unsafe code in C#. Period. I don't know why you're still going on about this.
    – Nick
    May 28, 2014 at 18:23
  • 3
    Also might be worth me pointing out - your code doesn't compile. You can't use AddressOf on an object like that; AddressOf only works with the name of a method.
    – Nick Shaw
    Oct 2, 2014 at 14:51
2

Not possible, since vb.net does not support unsafe code.

1
  • 20
    You shouldn't answer your own question by copying someone else's answer. Just accept Bala R's answer instead.
    – sfarbota
    Oct 13, 2015 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.