InstallerClasses and little-known gems.

Tags: .NET, C#, Installer

I'm writing an Installer class for an MSI installer project, and passing a checkbox's value from the MSI UI. I noticed that when I pass the checkbox's linked variable into my code, it's received as a string, and not only a string - the possible values are "1" and "0".

This is where I started cursing softly, because as well know, the bool.Parse method can only accept the strings "True" and "False" to parse, and not the commonly-found "1" and "0". Sure, I can put in a line such as this:

bool useSetting = Context.Parameters["MyParam"] == "1";

but you'll have to agree that it's much uglier than:

bool useSetting = bool.Parse(Context.Parameters["MyParam"]);

At least as far as I'm concerned. C/C++ veterans probably have no issue with the first one, but I take offense at this weakly typed pain.

Luckily, it would seem, the .NET class developers felt my pain, and decided that while this icky string comparison is necessary, there's no reason for me to see it. The InstallContext class contains a method called IsParameterTrue which does exactly this sort of string comparison, only a bit more robust than I would have bothered to write. So now I can just use this:

bool useSetting = Context.IsParameterTrue("MyParam");

and feel good about myself.

No Comments