Category: Programming


Posted by – July 5, 2008

I thoughtA blue boat Mark’s posting about the new Adobe Acrobat 9 was the hilarious. If you’re into more snark, try the Adobe Acrobat 9 installation guide as done by the MicroPledge site.

After the unpacking, the install process itself took 10 minutes. I could only thank Adobe’s engineers, presuming they were filling up my hard drive with yummy icons, tasty DLLs, and amazing 3D JavaScript add-ons. No matter — the 210 MB it required was there to be used.

I was doing some Delphi coding earlier this week and was reminded of the fact that the Borland Inprise Borland Codegear… OK, let me try that again. I was doing some Delphi coding earlier this week and I had noticed (before) that the former Borland tools were bought up by Embarcadero Technologies. I could say this nicely, but lets keep it friendly for now1: at least the designer of Delphi (and earlier Pascal compilers) ended up designing the C# language and .Net Framework (see also The Borland Conspiracy Theory). I only wished that the Pascal language designers had done away with the silly ‘assignment’ syntax (:=): you know, with this emerging emoticons craze on these Internets I could say it slightly resembles a guy growing a thick beard. Oh wait, this just came through my earpiece: the emoticons craze is so early 90s.

1 Though, programming in Delphi taught me to write solid leak-free code, I guess. Oh the joys of freeing up objects yourself

PropertyGrids in C#

Posted by – February 17, 2008

Yesterday, An example property gridI decided to tackle .Net’s PropertyGrid component: for Helios, I was working on code to allow users to view Datasource-specific data at run-time. For this, I already had several support objects programmed and ready to go, and what better to test them right in that PropertyGrid component?

This worked out easier than I thought: Basically, the only thing you need to do as a programmer is to actually create those objects and assign it to ‘SelectedObject’ property of the component. This works in most cases, however, I had a TServerPropertyObject contained inside a TDataSourceObject (half-pseudocode follows):

public class TServerPropertyObject
     public string GroupByBehaviour {get; set;}
     public string IdentifierCase{get; set;}
     // etc...

public class TDataSourceObject
     private TServerPropertyObject fserverproperties = 
             new TServerPropertyObject();

     public string ServerName {get; set; }

     public string DSN { get; set; }

     public TServerPropertyObject Properties { 
          get { return fserverproperties; }

Obviously, the idea is to have the properties object of TDataSourceObject to show as a ‘collapsible’ item in the PropertyGrid component. To achieve this, you need to add (those devilish) so called ‘property attributes’ (I believe these are the only things that makes .Net look evil). In our case of the collapsible item, we’re going to use the ‘TypeConverter’ class and pass it the ExpandableObjectConvertor type.

public class TDataSourceObject
     private TServerPropertyObject fserverproperties = 
             new TServerPropertyObject();

     public string ServerName {get; set; }

     public string DSN { get; set; }

     // Make this property expandable and let .Net
     // figure out what to do with the object.
     [ TypeConverter(
        Description("Server properties")]
     public TServerPropertyObject Properties { 
          get { return fserverproperties; }

This will let .Net handle (and figure out) what to do with the object itself: In my case the contained object only exposed simple types (like strings and integers). Naturally, you’ll have to do a lot more work (read coding) when your objects (and components) need specialized ‘property-editors’: for that, I have to refer you to the .Net documentation.

Update: See also here for more context and a larger screenshot (outdated though but still relevant).

CVS meet Enscript

Posted by – February 17, 2008

Last night, This sucks. TerriblyI was installing CVS on my Debian box. Actually, it was already installed: the CVSROOT was ready to go and the service itself had been listening since the early days. However, I never finished assigning rights to myself, so, with that done and by adding myself to the cvs group, I was finally able to check in my latest Helios sources using WinCVS (I despise TortoiseCVS1).

For work, I had once added viewcvs to the CVS repository engine: Viewcvs allows developers to view the repository via a webserver. Easy as is: During its install (apt-get install viewcvs), I ran into the normal installation checks and changes for this specific script: Most of them can be resolved by Debian’s viewcvs installer but (as usual) you should prepare yourself for some old-handy typework. The first action on your list is to add a couple of Aliases to Apache’s httpd.conf file (in my case I was only interested in one, the viewcvs ‘docroot’ parameter: I don’t like graphics2).

 Alias /viewcvs/ /usr/share/viewcvs/

The next step is to set the ‘docroot’ and ‘icons’ folder parameters in the viewcvs.conf file (/etc/viewcvs/): these folders refer to the folderstructure on the webserver and not to the physical folder structure on your Linux system (hint: check Alias). After this, you can set the use_cvsgraph and use_enscript variables in that same file to 1.

Enscript is a ‘source code highlighter’, but (obviously) it doesn’t come with a state file for C#: I found a good one at Adam Milazzo’s (Thanks Adam). This file should be copied to the enscript states directory (on Debian that is at /usr/share/enscript/hl). Also, you will have to make a couple of changes to the file (which is in the same directory): add the csharp highlighter to the namerules section. The final step that needs to be undertaken is a change to the file (you generally should be able to find this at or around /usr/lib/python2.3/site-packages/viewcvs/). Look for the enscript_extensions section. Notice the comment that one of the programmer left behind, just right above that extensions section:

### this sucks… we have to duplicate the extensions defined by enscript

You wouldn’t say.

1 Yeah, I was going to write something here, but I forget. Oh right, something about TortoiseCVS and how I hate it.
2 Uh. Right. What was that again?

Owner-draw (II)

Posted by – February 6, 2008

Earlier, An owner-draw gridwhich is, almost a year ago, I was comparing owner-draw programming in Delphi and C# and concluded that it’s quite similar: basically you set any visual component in a particular mode (‘I’m going to take care of drawing the content’) and then you basically override the component’s OnPaint/OnDraw event. Drawing the actual stuff is the hardest part, but there’s pretty good API help for both Win32 and .Net platforms. If you have programmed graphics in Win32 before, this is going to be a piece of pie.

So, yeah: a couple of years ago, I did some fancy stuff in a Delphi Drawgrid component. The .Net framework has a similar ‘grid’ like component, which is called a ‘Datagridview’ and you probably guessed it: it has basically the same functionality and offers the (almost) the same features as Delphi’s DrawGrid component. This brings me to that earlier mentioned project I was working on: a Sudoku puzzle generator, which obviously needed to use a custom grid-like component with a lot of colours (Warning: if you’re not interested in programming, you probably don’t want to continue reading).


Is there a statistician in the house?

Posted by – January 19, 2008

I downloaded the Visual Studio 2008 Express Edition ISO from the Microsoft site and discovered that specific stuff I was looking for isn’t supported in that special edition. I guess there is (or rather was) a huge difference between ‘Orcas’ and the Express Editions (Earlier at xsamplex).

That said, there was a time for looking through code and then I discovered that at Microsoft’s WPF site somebody had uploaded a Peer-2-Peer version of Sudoku (see also this post, or rather, this photo). This reminded me that a couple of weeks ago, I was asked to help with an algorithm that generates a complete filled out Sudoku puzzle. This I did and after that I moved on. So, that Peer-2-Peer version apparently has an algorithm that does the same, so when I opened the particular source file, I was shocked to find the following code (see my tag):

for (int iRow = 1; iRow < = 9; iRow++)
    for (int iCol = 1; iCol <= 9; iCol++)
        iNum = rnd.Next(1, 10);
        while ((IsInColumn(iNum, iCol) 
                  | IsInRow(iNum, iRow) | 
                             iRow, iCol)))) 
                  == true)
            // Generate a number to be put on the grid..
            iNum = rnd.Next(1, 10);

            // AH01 -- OH NOES...
            if (iAttempts == 100)
                bStuck = true;
            // AH01 -- Ends.
        if (bStuck)
        iAttempts = 0;
        arrPuzzle[iRow - 1, iCol - 1] = iNum;
        if (iRow == 9 && iCol == 9)
            bDone = true;
    if (bStuck)

Let me go back a couple of steps. In Sudoku, a 9 by 9 grid is divided in nine 3×3 grids: the numbers in each 3×3 grid MUST be unique and between 1 and 9. Additionally the same rule applies to each horizontal and vertical line in the grid. A fairly good description can be found at Wikipedia.

The main algorithm for filling out a 3×3 grid with 9 unique numbers, is a statistical routine that should have came up during math classes as the ‘coloured balls in a basket problem’. There’s 10, no, lets say 9 different coloured balls in a basket. What are the chances of (blindly) picking out the red ball in the second turn?

Back to that source snippet: Random generating a number from 1 to 9 The author randomly draws a number between 1 and 9, checks if the number has already been drawn. If so, he keeps generating a random number until he gets a number that fits the needs. Here’s where the problem starts: the more numbers you have picked, the less chances there are going to be that your random generator is going to pick one of the numbers that has not been drawn yet. Take a look at the example on the right (an animated gif, opens in separate window): If the stars are aligned right and your computer is in a bad mood, it may just take a while to randomly generate those last numbers. Certainly, you could throw in a 8-core computer to speed up the random number generator but the problem will still be there. The programmer knew this too hence the reason for that ‘jailbreak condition’ (“After 100 attempts I think I should break it off otherwise it looks like the program has crashed”).

Randomly drawing numbers from 1 to 9The right way to do this is, by actually drawing these numbers from either a list or an array as shown in the animated GIF (click on the image on the left). This effectively means that your randomize Next routine needs to adjust dynamically after each draw: after all, every time you draw a number, the amount of available numbers decreases by one. There’s a minor catch here, but that’s up for you to figure out.

So: what’s the point of bringing this up? The Sudoku Peer-2-Peer application is a great showcase of the latest and greatest .Net technologies but fails horribly on the basics: using math to solve a complex problem. I do (for example) have more respect for this guy who threw in a genetic algorithm to solve the problem. This is overkill for sure because it can take up to 10 minutes to fill out a complete grid. At least he understands (and showcases) how genetic algorithms work.

Update (01/21/08): Related: Who killed the Software Engineer?


Posted by – January 11, 2008

A couple of days ago, it was the 70th birthday of Donald Knuth (or rather, his webpage), who as you found out, is hero to many programmers around. I think he’s still working on completing his masterwork, ‘The Art Of Programming’. Anyway, his birthday has not gone unnoticed in the blogosphere. It’s almost like everybody knows each other. No, really. Really.

That brings me up to something (slightly) related: I was using DailyMotion to find older clips of the 80′s band Blondie and the search result of that site always seem to include references to movies with blondes; some of them quite, lets say, exquisite. I won’t link to a URL of such, but I encourage you try it out. Obviously, DailyMotion is using a ‘soundex’ routine: this is an algorithm that indexes keywords by sound based on (language-specific) rules. This works brilliantly for looking up people’s last names (or even first names) but not for searching specific terms like the example I mentioned earlier.

Seriously:If I’m searching for ‘Blondie’, I’m expecting results for Blondie and not ‘Blond’, ‘Blonde’, ‘Blondes’, ‘Bland’, ‘Britney Spears’. And definitely not ’2 h0t bl0nd3s k1ss1ng 3ach 0ther’1.

1 Obviously, I used ‘some encryption’ there to ensure that your kids don’t end up on this kids-safe website when they google for ‘Blondie’. You’re welcome.


Posted by – January 8, 2008

A couple of months ago (around the same time that I was looking into the sources of the .Net version of KeepPass) I ran into this page: an algorithm to generate pronounceable passwords1. Well, at least a bit more pronounceable than the ones generated by password generators like the one that is used in KeepPass.

The original code was written in Java (by Thom van Vleck, if I’m not wrong) and it appears that there’s also a C++ version available.

Not to be outdone by these formidable programmers, I converted most of the code to C#: I don’t dare to say ‘porting’, which it obviously isn’t. However, I made drastical changes to how the GPWData class is being called: all of the functionality now is IN this class (including password generation and initialization). This makes more sense and it’s also a bit more elegant to use, as shown in the following example:

            GPWData npdata = new GPWData();            
            npdata.GeneratePasswords((int)numPasswords.Value , 
                (int) numPasswordL.Value);
            foreach (string s in npdata.Items)
                textPasswords.Text += s + "\n";

I dumped the code in the Public Domain which means that you can use it for whatever you wish to use it as long as you credit me and the original authors. Go grab the sources. The zip-file only contains the sources: I generally don’t add executables to full-source projects. Also, the target platform is .Net 2.0.

1 From that Multics page: The routine was modeled after Morrie Gasser’s ‘Random Word Generator for Pronounceable Passwords’. The original routine was then standardized (adapted might be a better word) by the NIST

Oh. Noes!

Posted by – November 15, 2007

Yesterday, my two sets of Ubuntu 7.10 (“Gutsy Gibbon”) arrived by snail-mail, which means that I’ll be upgrading my ‘v1r7u41 b0xen’ this weekend. Once again, the package came with free stickers. Hey, Sun, what’s that? Still no free stickers with your Solaris disks?

Engadget listed PhotoVu’s Digital Frame which comes with an RSS reader. Wait, there’s more: it has a built-in webserver (so that you can set up things and stuff). Wait there’s more: It uses Samba. 1, 2, 3, 4: Why, it runs Debian, of course. Nifty.

And on a completely different note: the .Net Framework (confirmed in 2.0, for sure) doesn’t support serialization of TimeSpans. This is not really a big issue, because you can generally workaround this. It just struck me by surprise.


Posted by – September 2, 2007

I was looking for dictionaries the other day after finding myself impressed by Ubuntu’s Dictionary program. I found out that it actually uses the gdict set of XML files, and you can find around the freedict site (you probably need to scroll down: look for Dict Databases (Webster’s 1913). The dictionary is in an awful XML format though. Sample Perl-code is provided too.

This is also where I found the WordNet lexical database for English language: a C# interface can be found at ebswif’s (GPL). Not cleanly written code, though, but if it strikes you fancy, the source might be with you. (WordNet files are binary files, though).

Dictionaries, huh? A good friend of mine had a theory that good programmers generally have a knack for math and language. I always thought there was some truth in that.

Copy, Paste.

Posted by – June 21, 2007

Via something, I ran into this (online) sarcastic presentation about Copy Paste Programming (Wikipedia). A quick summary:

Best practices: Find the code, copy/paste it, compile, enjoy!
Pitfalls and workarounds: The copied/pasted code does not compile. Copy/Paste larger portion of code.

This reminds me of an actual encounter many years ago, where I was called up by an analyst who was struggling with his code. Upon asking what was wrong, he said the code didn’t work and that it was someone else’s. When I inquired where he had the code from, he said he had it copied and pasted from an existing (and live) application. Right: the context. That wasn’t the worst part, though: when glancing over his design document I found out that it literally instructed him to ‘copy the code from this-and-that application’. I’m not sure who wrote up that document, but obviously someone was in a rush, which evidently came up when I asked the question why he would copy and paste a complete piece of code from another application.

The answer he gave was solid gold.

Hey, hey.

Posted by – June 14, 2007

Today, I discovered that the StringReaders/Writers in .Net only support UTF-16 encoding, which may lead to some breakage if you’re using it in combination with the XMLSerializer object.

If you really need to maintain UTF-8 compatibility, you should use a MemoryStream. This is actually just as simple as using a StringReader:

  MemoryStream m = new MemoryStream();
    XmlSerializer formatter = 
        new XmlSerializer(typeof(TMakeUpYourMind));
    formatter.Serialize(m, this);
    m.Seek(0, 0);
    StreamReader rd = new StreamReader(m);
      res = rd.ReadToEnd();

Don’t forget to ‘rewind’ the stream to position 0.

I’m not sure why this isn’t mentioned in the SDK. Also note to the Visual Studio Express developers: please make this weekly ‘registration of Visual Studio Express’ on my computer stop. I’m sure it’s a bug because everytime I register I get the same e-mail in with the same deals. I’ve already got 12 of them. Please.

MT, ToolTips

Posted by – June 5, 2007

You may have noticed that version 4.0 of Movable Type has been officially ‘open sourced’ (at Digg). I prefer the rather more correct ‘Movable Type has been relicensed under a less restrictive (or more restrictiver, whichever you fancy) license’. After all, the MT package has always included the code.

Does MT still matter these days? They do but not as much after that famous ‘relicensing’ hot-discussed event in 2004. A lot of people have moved to WordPress since that: however, I thought the generation of static pages was one of the plus-sides.

To .Net then: if you use the ToolTip class and create instances dynamically you may have noticed that the class showcases a design paradox: if you don’t dispose these instances after you’ve finished using them, you’ll end up with (what Microsoft calls) a ‘managed resource leak’ (obviously, there is a limitation of GDI objects a program can create. My favourite part is ‘There is a theoretical limit of 65,536 GDI handles per session’). However, a timed ToolTip (the type that disappears ‘magically’) obviously has ‘visibility’ issues when being disposed too soon (it will never pop-up). So, the questions is: When do these tooltips need to be disposed then? It looks like that’s a paradox unless you drop them in your project at design-time.

That, SerialPort stuff

Posted by – May 29, 2007

The log files reveal that a lot of people are looking for help using the Serialport component of the .Net Framework. Once again, lets summarize:

  • There are two ways to access data from a serialport:
    1. Using the DataReceived event (non-blocking)
    2. Using (individual) ReadLine statements in your code (blocking)
    For a more detailed description, I mentioned the difference in a different posting. Which method you’re going to use depends on how or when data becomes available to you and where you want that data to appear in your application. If your GUI needs to be updated frequently, use non-blocking. Note that you can mix ReadLine statements in your DataReceived events, but most likely you will need to clear your serial port buffers before closing off the program.
  • Know the difference between the various way of reading data (Read, ReadLine and ReadExisting). Experiment with them.
  • If you need to read a certain amount of bytes, check into the BytesToRead property.
  • If you’re going to use blocking methods, make sure to adjust both read and write timeouts appropriately.