The views expressed on this weblog are mine alone and do not necessarily reflect the views of my employer.
 Tuesday, October 08, 2002

Top subtle (as a brick in the face) issues when converting my Tiny OS from C# to VB.NET:

  1. Array Lengths...I knew we were warned, and there were all those arguments from Beta1 to Beta2 to RTM, but still...

         byte[] bytes = new byte[4];

    has length 4, from 0 to 3

         Dim bytes(4) As Byte

    has length 5, from 0 to 4

  2. Integer Divison... "/" and "\" are different operators in VB.NET than C#.  "/" doesn't round, while "\" does...

         (uint)(boundary * ((number / boundary) + ((number % boundary > 0) ? 1: 0)))

    where boundary is 16 and number is 82 returns 96.  While "equivalent (not)" VB.NET

         CType(boundary * ((number / boundary) + IIf(number Mod boundary > 0, 1, 0)), Integer)

    where boundary is 16 and number is 82 returns 98 because (number / boundary) returns 5.25, not 5.  This was fixed by using a backslash.

         CType(boundary * ((number  (BACKSLASH) boundary) + IIf(number Mod boundary > 0, 1, 0)), Integer)

    This is one of these obvious, silly things you've known since VB3, but you don't think about it when converting from C# to VB.NET. 

  3. UInt32 isn't supported in VB, so I had to wimp out and switch to Integers.

Updated Link to this post 4:22:11 PM  #    comment []  trackback []

And the answer shall come...this is it.  This is why I love the hell out of .NET.  I tell this to my students when I teach .NET, but each day I use the Framework I start to live it even more.  Sure, there are things you fight with, there are things you hate, but really when it comes down to it: A LOT of good thought was put into the Framework.  There are Utility Classes galore.  (Of course, there's no HashMap, but that's another day)

What I did in a cheesy moment (a 3am moment) of frustration:

public unsafe static byte[] UIntToBytes(uint UIntIn)
    //turn a uint32 into 4 bytes
    byte[] fourBytes = new byte[4];
    uint* pt = &UIntIn;
    byte* bt = (byte*)&pt[0];
    fourBytes[0] = *bt++;
    fourBytes[1] = *bt++;
    fourBytes[2] = *bt++;
    fourBytes[3] = *bt++;
    return fourBytes;

Here's what it looks like now (in VB.NET):

Public Shared Function IntToBytes(ByVal IntIn As Integer) As Byte()
Return BitConverter.GetBytes(IntIn)
End Function

I can't believe I stooped to writing unsafe :) code to do something as simple as getting the Bytes out of an Integer.  Fool me once, shame on you.  Fool me twice, shame on me.

Updated Link to this post 3:25:08 PM  #    comment []  trackback []

Here's an interesting thing Jon Udell found...a POP/SMTP Proxy and Web Server that Googles and Blogs your email. Seems like a thinner/cleaner/smarter version of Enfish.

Googling your email. ... [Jon's Radio]

Updated Link to this post 2:33:34 PM  #    comment []  trackback []

Well, I got the big idea that I'd convert my Tiny Abstract OS in C# to other languages (VB.NET, J#, etc)  and platforms (.NET CF, Mono).  I figured it'd be a good exercise to practice VB.NET since it's moderately complex and moderately OOP.  I've never been one who can bang out little samples - I really need something cohesive and meaty if I'm going to really dig into it.  Plus, it'd be cool to have other versions of the TinyOS around in other languages. It really exercises the basics of the .NET Framework, and it's been downloaded 4955 times from, which is very exciting and I get mail about it all the time.

So, I figured I'd start with VB.NET before I fire up VMWare and my Mono VM.  I sat down and started using the C# to VB.NET converter VS.NET Add-In.  I converted the who project and received 102 Todos, which is about more than I expected.  I had no idea what was really going on...until I saw things like this in my new VB Project:

If instr.Param2 <> System.UInt32.MaxValue Then 'ToDo: Unsigned Integers not supported


Public Sub New(id As System.UInt32, memorySize As System.UInt32)
'ToDo: Unsigned Integers not supported
'ToDo: Unsigned Integers not supported
pid = id
registers(8) =
CType(pid, System.UInt32) 'ToDo: Unsigned Integers not supported
processMemorySize = memorySize
End Sub 'New

Turns out, from a CLS point of view, I really shot myself in the foot on this one.  I wrote this Tiny OS using exactly what I needed - when the system called for UInt32, I used one.  Apparently (I knew this, but never really grokked it) the CLS does not define an unsigned integer, and the UInt32 type is marked as not CLS compliant. So, what type should I use?  It was a silly thing really, to build something low-level bits-and-bytes with a managed language, but since I was at a low level, it seemed silly to only use the signed half of an Integer.

Well, I'm going to re-think this little exercise, perhaps I'll first try to get it running as a C# app in Mono and go from there.

Let me add one particuar point of note.  I'm interested in this valiant attempt by the C# to VB.NET Converter and appreciate any insights.  When trying to convert this particularly icky ( that I look at it, I could have done it easier with adding the bytes and shifting bits as I added...ah, how time refactors for you...) bit of C# code:

public unsafe static uint BytesToUInt(byte[] BytesIn)
     fixed(byte* otherbytes = BytesIn)
uint newUint = 0;
uint* ut = (uint*)&otherbytes[0];
     newUint = *ut;
return newUint;

The C# to VB.NET converter came up with this:

Private Declare Sub CopySystem.UInt32 Lib "Kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As System.UInt32, ByRef Source As System.UInt32, ByteCount As Integer)

Public Shared Function BytesToUInt(BytesIn() As Byte) As System.UInt32 'ToDo: Unsigned Integers not supported
Dim otherbytes As Machine.BytePtr = New Machine.BytePtr(BytesIn)
          Dim newUint As System.UInt32 = 0 'ToDo: Unsigned Integers not supported
Dim ut As Machine.TypePtr = New Machine.TypePtr(Machine.TypePtr.Cast(Machine.NewPointer(otherbytes(0)), GetType(System.UInt32)), typeof(System.UInt32)) 'ToDo: Unsigned Integers not supported
CopySystem.UInt32(newUint, ut.Address, Runtime.InteropServices.Marshal.SizeOf(GetType(System.UInt32)))
Return newUint
End Try
End Function 'BytesToUInt


Updated Link to this post 1:42:47 PM  #    comment []  trackback []