0

I have a method where I'm passing in a string:

 void MyMethod(string someVar) {...}

The string value is assigned from the configuration file:

ConfigurationManager.AppSettings["MyConfigKey"]

I would like to replace string with something that is typed so I know that parameter is a specific config key value:

void MyMethod(MyConfigKey someVar) {...}

But there are several of these config keys and I'd like to some how group them.

I was thinking of an enum such as:

public enum MyEnum
{
  ConfigKey1,
  ConfigKey2,
  ConfigKey3
}

But then I'd be doing enum conversions all over the place. Some of the config keys are bool, some string, etc.

I could do a static class:

public static class MyClass
{
  public static string ConfigKey1 {get,set}
  public static bool ConfigKey2 {get,set}
  public static string ConfigKey3 {get,set}
 }

But then the parameter in MyMethod() is still a string, leaving the door open for any string to drop in.

Any ideas how to have a typed parameter for this scenario so a regular string cannot drop in?

4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • 1
    This is an intrinsic problem of AppSettings. This is one of the main reasons to use the applicationSettings or userSettings where the net configuration Subsystem handle these values in a typed way – Steve Jun 18 '13 at 20:34
  • I may not be following what you are trying to do, but check out `Reflection` and this link: http://stackoverflow.com/questions/3862226/dynamically-create-a-class-in-c-sharp – Nikita Silverstruk Jun 18 '13 at 20:40
  • It almost sounds like you're attempting to do something like a C++ union. Is that what you're looking for? What's your concern with someone being able to pass in other types of strings? – Maurice Reeves Jun 18 '13 at 20:47
  • Sounds like you are trying to re-invent the Properties + Settings tab. It already does what you are asking for, generating type-safe and named properties in the Properties class. – Hans Passant Jun 18 '13 at 20:57
  • @Steve: Do you have an example of that? – 4thSpace Jun 18 '13 at 21:42
  • @MauriceReeves: The code in that method will execute only if a config key value is passed in. So if someone passes in "somestring", nothing happens. I want to avoid that ever happening since there's the potential for a time consuming bug. – 4thSpace Jun 18 '13 at 21:44
  • 1
    Not easy to give an example here, but open the properties of your project, go to the settings page and create the settings file, then declare your config using the provided editor. (applicationSettings), now look at your app.config file, you should see the properties with the exact definition of the type. Now in your program you could use Properties.Settings.Default.YourProperty without the need to convert from string. You could also use a Custom Configuration Section as explained here http://www.codeproject.com/Articles/16466/Unraveling-the-Mysteries-of-NET-2-0-Configuration – Steve Jun 18 '13 at 21:56
  • @Steve: Ok, yes. Much better. But that doesn't get around being able to restrict the string parameter in MyMethod(). You have some idea on that? – 4thSpace Jun 18 '13 at 22:30

0 Answers0