Dan Byström’s Bwain

Blog without an interesting name

Archive for December, 2004

.PNG + .NET

Posted by Dan Byström on December 14, 2004

The choice of .PNG as the file format for your Windows Forms graphics is superior to .BMP, .GIF, .JPG and .ICO.

The proof of this statement is left as an exercise to the reader. (Hint: Alpha channel.)

Posted in Programming | Leave a Comment »

Quest Completed

Posted by Dan Byström on December 8, 2004

10,000 experience points gained.

I’ve done some serious searching in the .NET help files as well as hard core googling to find out how to do design time interaction with a UserControl. I thought this would be one of the first tings everyone would like to try out in .NET. 😉 But it was amazingly hard to come up with the relevant keywords. I found numerous of really really exciting stuff along the way, so this time was by no means wasted.

This is a reminder of how easily we (normally) can find information these days. Back in the eighties information was found in magazine articles, in books arriving three months after ordering them and by disassembling core products. Not a day passed without tracing and disassembling parts of MS-DOS and the BIOS. Those were the days.

In the nineties, all of a sudden, it was possible to use CompuServe to get in touch with other people to find answers! Then came news servers and finally the whole thing just exploded.

It was way back then, when I got a strange thing tossed in my hands, called MFC. It was called a framework and used some buzz-word technology called object orientation. Surely nothing a real programmer can benefit from I thought (and was proven right, right?), but anyway it was raining and I had nothing better to do so I did something that I had wanted to do for more than ten years: I wrote my own Qix-clone. What? Did I notice you shake your head and smacking your tongue??? You’re obviously no geek at all and shouldn’t be here! Now; just leave! Go on! 😉

The concepts of inheritance, abstract classes, virtual methods, overloading, overriding, aggregation and so on were all completely new to me, but I immediately felt totally at ease with them. I was thrilled realizing that, after creating my monster class, I could just inherit from it in order to get my fuse! Way cool I thought! I created my game vector-based and had to come up with a lot of vector manipulation routines. How do you calculate the area of a (closed) polygon? How do you find out if a point is inside or outside a (closed) polygon? I found the answer to the latter question and was immensely proud of it. I later found out that it was a well known algorithm, but anyway… nothing beats doing it yourself.

Qix remained on some backup disk for some years until another buzz-word appeared: Java Applets. Converting the MFC C++ code into Java wasn’t that hard, and boy was I lucky that I hadn’t relied on the Windows API to help me with regions. How do you fill a polygon with a bitmap without any framework support? Go figure! 😉 vdQix can still be played here, at some periods it has had more than 10,000 hits a month. But this story is not about Qix, instead it is the story of my “point inside a polygon” algorithm.

At roughly the same time, I was working on a VB project where we wanted nicer looking buttons than Sheridan’s controls bundled with VB3 could accomplish. We bought a package calls VB-Tools (more popularly called VB-Fools) which contained a vast number of controls. Each being so poorly designed and full of bugs that I got irritated enough to roll my own.

I ended up with a button control, a tooltip control, a control to monitor processes and a hotspot control (DBPush.vbx, DBTTip.vbx, DBAppMon.vbx and DBHots.vbx). They came out pretty well and I uploaded them to various places and they spread like wildfire. For years I had daily conversations with happy users. A few quotes can be found here. Very rewarding indeed. They almost even made it onto the MSDN CDs!

The DBHots.vbx was directly inspired by my “point inside a polygon” algorithm developed for my Qix game. It relied upon the fact that a VBX was able to interact with a user at design time and that a VBX could be transparent. VBXs were just great in their simplicity! Then along came ActiveX with 32-bit VB4 and both these features were gone! With VB5 and the OCX96 revision these features were put back in, but then it was too late. No ActiveX version of DBHots ever saw the light of day.

Now we’ve come full circle in explaining why I would like to do mouse interaction with a UserControl in .NET. Some things may lurk for years, waiting for the right moment. Creating a .NET version of DBHots is something that simply must be done before I can rest. The next thing I’d like to know if it is possible to have my UserControl not being created as a control at all at run-time, but rather become a component so I don’t have to waste a Window handle on it.

The class I was looking for? It had the most obvious name imaginable: ControlDesigner.

Posted in Nostalgia, Programming | Leave a Comment »

With Zwei.Drei.Vier.Links()

Posted by Dan Byström on December 1, 2004

In preparation for a continued discussion of how a language could assist us in checking for null references, I’ll state what I see as drawbacks with the With-statement in VB (or Pascal for that matter, where it has its origin). Eh, say that again? Well, it’ll become clear later on.

In VB6 I used With a lot. In the world of COM it was not merely a convenience, but also an optimization technique, since the programs ran faster when calls to AddRef/ReleaseRef were reduced. In .NET that’s not an issue anymore.

Anyway I still miss it in C#.

Consider the following piece of VB code:

With Customer(42)
  With .Order(3)
    .Lock()
    Process .Order(3)
    Customer(42).Update
    .Unlock()
  End With
End With

Never mind the object names or why you’d like to do exactly something like this. I’m only interested in the implications made by the With statements. We can observe that:

  1. We can only have one “active” With at a time, so nesting them won’t help us access the outer With. Nested Withs also makes the code hard to read.
  2. If we have the need to pass the “withed” (I don’t have a better name) object down to a method (like the Process method in the example above) we have to reevaluate the “withed” object.
  3. Entering a With block with a null/Nothing reference is completely and utterly useless, yet it is allowed, and the exception won’t occur until we try to access the first method/property of the “withed” object.

Given those drawbacks, it may be understandable that the With statement wasn’t made part of the C# language. I think 1) and 2) are quite obvious, but 3) is more my own personal reflection and something I want to improve upon!

What if we could overcome all three issues? Stay tuned…

Posted in Uncategorized | Leave a Comment »