It's time to force my own hand.
Over the last few weeks my interest in figuring out the inner workings of code has seemed to come to the fore. Most specifically, I've been looking at a lot of IL lately.
So I've decided to do something about it. So far, I've played a little, dabbled a bit, but mostly I've read the IL generated from VB or C# code I've compiled in Visual Studio. Doing this let's me understand small little pieces of code, and I've sucked up a fair bit by logically figuring things out based on before and after shots.
But I haven't tried to learn. Not really.
So I'm starting a quest. I figure if I declare it publicly, I might actually even see it through.
I'm going to write myself a piece of code in VB. It's not going to be uber-complex, but it's going to cover a widish variety of language features. I'm going to try to avoid as many implicit calls into the Microsoft.VisualBasic namespace as I can, so that I'm only doing stuff that is supported natively by the framework - that is, I'm only going to call functions that are somewhere in the System.* namespace area (so no custom library calls).
And then I'm going to sit down and teach myself what I need to know to write the whole thing again a second time, but this time only using IL as my language of choice. And I promise, I won't cheat. While I'm working on this, I'm not going to take my original VB code and read it in reflector or ildasm :)
To be honest I don't know why I haven't done this earlier. Probably for the same reasons I never sat down and learned assembler properly - there's only so much room in my brain at once :) Actually, I find IL a lot easier than assembler - and probably quite rightly so, for after all, assembler deals with all these different registers and address spaces and stuff. IL is a simple stack machine.
It's now or never though I guess. I have no idea, but I won't say I'm surprised if there's some new things to learn when the 2.0 framework comes out in 6 months - generics for starters, perhaps? So I better get this sorted out now, so that when we hit version.next I understand what the changes mean, if there are any.
I'm not going to post the full source of my VB version - you can read that here. For now, I'll just list the main things happening inside it that I wanted to be sure I included.
- Some Interfaces.
- Object Inheritance.
- ByRef as well as ByVal
- ParamArrays
- Outputting to the screen.
- Calling some other library (System.Math)
- Making an executable (not just a class library).
- Member level variables.
- A property.
It's not fantastic code. What I was trying to get out was some things to get implemented, not good or useful or well written code that had error handling. If I can get this one done, then I'm going to make sure I have a Try Catch block, and maybe see if I can do something like implement a web service :)
I don't know if anyone else will find this useful, but as I teach myself interesting bits and pieces, I'll make sure that I describe what's going on for you all, so you can either a) learn something too perhaps or b) laugh at me for doing it so wrong. Either way it's a useful outcome :)
Hopefully it doesn't take me too long...