Scott Hanselman

Top Subtle As A Brick In The Face Issues When Converting My Tiny OS From C To VBNET Array LengthsI Knew

October 8, '02 Comments [0] Posted in Web Services
Sponsored By

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.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

And The Answer Shall Comethis Is Itnbsp This Is Why I Love The Hell Out Of NETnbsp I Tell This To My Students When

October 8, '02 Comments [0] Posted in Web Services
Sponsored By

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.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Heres An Interesting Thing Jon Udell Founda POPSMTP Proxy And Web Server That Googles And Blogs Your Email Seems Like A

October 8, '02 Comments [0] Posted in
Sponsored By

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]

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Well I Got The Big Idea That Id Convert My Tiny Abstract OS In C

October 8, '02 Comments [0] Posted in Web Services
Sponsored By

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 http://www.gotdotnet.com, 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

and:

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 (hacky...now 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)
    
Try
          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
    
Finally
         
otherbytes.Dispose()
    
End Try
End Function 'BytesToUInt

Hmm...

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Couple Of New Books Over At My Bookstore DiabeticBookscomnbsp Check Them Out

October 7, '02 Comments [0] Posted in Diabetes
Sponsored By

Couple of new books over at my bookstore, DiabeticBooks.com - check them out!

Guide to Healthy Restaurant Eating, 2nd ed - Hope Warshaw

This best selling book, now in it's bigger and better second edition, is just the resource you need if you frequently eat restaurant meals. If you are like most Americans you eat out at record breaking frequency and know, only too well, the hazards of restaurant foods-fat and sodium count and overwhelming portions. You can still eat restaurant meals, you just need to learn the skills of healthier restaurant eating and to get the low down on the nutrition data of restaurant foods. Guide to Healthy Restaurant Eating helps you do just that.

You'll get the nutrition low down for over 3500 menu items at more than 55 of the best known restaurant chains across America-McDonalds, Boston Market, Denny's, KFC, Pizza Hut, Taco Bell, Dunkin' Donuts and more. In addition to calories, you'll find carbohydrate, fat, saturated fat, cholesterol, sodium and more as well as the diabetes servings or exchanges.  

The Guide to Healthy Restaurant Eating also helps you learn the pitfalls of today's restaurants, strategies for self-defense, and the keys to healthy eating. Plus you'll get hundred of tips to estimate restaurant portions.

Simply put, this book is an indispensible resource for people who eat out regularly.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.