views:

76

answers:

3

It can be either at compile time or at run-time using a config file. Is there a more elegant way than simple (and many) if statements?

I am targeting especially sets of UI controls that comes for a particular feature.

+3  A: 

Unless your program must squeeze out 100% performance, do it with a config file. It will keep your code cleaner.

If one option changes many parts of code, don't write many conditionals, write one conditional that picks which class you delegate to. For instance if a preference picks TCP versus UDP, have your conditional instantiate a TcpProvider or UdpProvider which the rest of your code uses with minimal muss or fuss.

Kevin Conner
A: 

Perhaps I am assuming too much, but:

switch (setting) {
  case "development": 
     dostuff;
     break
  case "production":
     dootherstuff;
     break;
  default:
     dothebeststuff;
     break;
}
lordscarlet
+2  A: 

Compiler directives aren't as flexible, but they are appropriate in some circumstances.

For instance, by default when you compile in DEBUG mode in VS.NET, there is a 'DEBUG' symbol defined...so you can do

void SomeMethod()
{
    #if DEBUG
    //do something here
    #else
    //do something else
    #endif

}

this will result in only one of those blocks being compiled depending if the DEBUG symbol is defined.

Also, you can define additional symbols in Project Properties -> Build -> Conditional compilation symbols.

Or, via the command line compiler using the /define: switch

James