Jump to content

February 2017 »

1920212223 24 25
- - - - -

Learning C#. Whee.

4: Adsense

This afternoon I got back from Quiznos and decided to slack off for a bit before going back to work. (On a totally unrelated note, walking a 3 mile round trip and getting a sandwich out of the deal is absolutely awesome. Do it often if you at all possibly can.)

So about 1:25 I decided to have a shot at this whole C#/Habanero thing. Got to start somewhere, right? So I fire up my trusty Visual Studio 2005 and create a new Windows project in C#.

The first thing I notice is that the IDE has created a form for me. This is familiar from my glory days doing applications programming in VB. They still call the thing "Form1" for no apparently good reason, which has bugged me all the way back to VB3. I swear, VC++'s wizard is so much nicer: it asks you what you want to call your classes. Why they've never done that in VB, and now C#, is totally beyond me.

Anyways, that's just an idle curiosity, and easy to get past. I blow away the form since I'll want to dynamically create my render windows on the fly. If I need prefab forms later for config UI and such, I'll build them - but for now I'm going with pure code.

Goal #1: Get a message box on the screen
I figure this is a good place to start: really trivial, but probably different enough from the stuff I'm used to to provide a learning experience. Sure enough, I fumble around for about 10 minutes trying different magic incantations involving System.Windows.Form.MessageBox. I try treating a MessageBox as an object and statically allocating one. I try dynamically allocating one. For kicks I poke around in IntelliSense for a second and fail to find what I'm after.

Finally I break down and just Google "C# messagebox" and wham, there's the answer: MessageBox.Show(). Duhh. I guess this .Net stuff is going to be more of a mental paradigm shift than I thought [grin]

So, drawing on my highly limited and utterly broken Spanish, I proudly accomplish Goal #1:

Hello, world! ... sort of.

Current time: 1:39 PM. Yayy.

Goal #2: Get a dynamic window displayed
This is my second big test. Judging from experience with C and C++, this is probably going to take a while, so I mentally steel myself for going back to work as soon as I'm done on this phase.

When I tore out the prefab form from my new project's code, I noticed the Application.Run() call, which apparently is .Net's ready-made message pump loop. It can take an object of type System.Windows.Forms.Form. So I poke things a little bit, slap together some code, and produce this:

Form foo;
foo = new Form();
foo.Text = "Habanero";
foo.SetBounds(0, 0, 225, 150, BoundsSpecified.Size);


It takes a few seconds of juggling IntelliSense to figure out the correct parameters, but this time I get by without needing to touch Google or even MSDN. It literally takes more time to write this paragraph documenting my attempt than it took to write the code.

My dynamic window in action

Current time: 1:51 PM.

Hot damn... now that's what I call rapid development. I think me and C# are going to get along very nicely.

This brings back my (very happy) memories of whipping up GUI tools in a matter of minutes in VB, back in the day. In fact, I still use VB fairly regularly to do quickie tools, like one-off file parsing/mangling utilities. It's a great thing to have in the toolkit, and I find the visual GUI design tools more than justify the fact that it's VB and not, say, Python. I've still got a very soft spot in my heart for VB; even though it supposedly teaches all these bad programming habits, my opinion is that bad programming is the fault of the programmer, not the language. I've seen some truly elegant and beautiful code produced in VB - although, admittedly, not very much.

C# though... I sort of feel like a dirty tramp for even saying this, but I think it's going to replace VB (even VB.Net) as my personal RAD tool of choice. The syntax very closely resembles the C++ and KC code I spend my time thinking in, and I'm not smart enough to deal with massive syntax shifts on a regular basis. When I do use VB or other languages I find my muscle memory putting semicolons and curly braces all over the place, which naturally causes some havoc.

C# also has delegates, which are just plain hot. I can already see entire aspects of my planned engine structure getting reduced to extremely simple, clean pieces of code by the mere power of delegates. I wrote a 500-line hacked imitation of delegate behavior for a customized callback system in C++ for the last Habanero attempt; it used multiple inheritance to do its work. With delegates I get all that power, plus more, without any code overhead of my own. I also get anonymous delegates, which make me want to jump around and giggle like a schoolgirl on crack.

So there we go... 35 minutes and I've finished a passable dynamic window dummy program, and written a journal entry about it. If I had any doubts before about this decision, they've been totally blown away.

This is going to be a whole heck of a lot of fun.

Note: GameDev.net moderates comments.