Strongly typed constant parameters in C#

After a bit of searching, I found a way to have strongly typed constant parameters for C# functions. You know the situation, where you need to pass one of a limited set of strings or chars or other values to a function and you want to make sure somebody doesn’t just go and pass any old thing they find laying around the place. Enums are pretty good for this kind of thing, but it gets hairy if you need to translate it to anything else, like a string or a char.

Any solution also needs to pander to intellisense, making it easy to use and kinda idiot safe (I’m talking about myself a couple of hours after defining any constant, which usually leads to me behaving like the idiot user I had a hard time envisioning just hours earlier).

I think I found a good system for doing this, and as an example, I’ll invent a function that takes a string parameter, but it has to be just the right kind of string. To do that, I first declare the constant strings in a separate module this way:

Then I write my function, the fictional “Rechandler” that takes a parameter of the ConstRecTypeValue kind. And then I write a function that calls it. Now, while writing the caller, I want intellisense to do its thing, and it does:

As you can see, it obediently pops up a tooltip to tell me only a ConstRecTypeValue is accepted here. As soon as I start to type that, it recognizes the ConstRecType static class name and it intellisensively lets me choose which constant member I want:

…which I complete the usual way:

The callee (Rechandler) then easily recovers the string that is hiding inside the passed value (in this case “DELETED”) and continues its merry ways.

Naturally, you can use chars, doubles or entire collections of values instead of the string value in this example and still achieve the same effect.

You can also take it one step further along the path to universality, by using a generic base class for the value type:

If you have this guy in reach in your project somewhere, you can now simplify the definition of the value class like so:

…while everything else stays just the same.

I love it.

Reflect on those constants

This falls in the category “neat tricks” and definitely under “DRY” (Don’t Repeat Yourself). When you have a list of constants that you need to save or retrieve, typically settings, you easily get into a situation where you have say 20 constant strings defining the names of your constants, and then a block of code going through the same 20 variables to retrieve or save them. When you add a constant, you’ve got at least three places to add code and then I’m not even counting the places where you actually use the settings value.

But using reflection in C#, you can easily make it so the system retrieves all your constants and their values into a dictionary at runtime and saves them back, using nothing but the declaration of the string constants.

This is an example of a declaration of the names of the values we want to save and restore:

And this is code that then gets the values of those constants and sticks them into a dictionary in runtime. The rest of the code is trivial and not worth reproducing here