0

well, let's say i've got the following if statement:

    if (a)
    {
       // do something.
    }

is it possible to run the statement only if a certain value in the app.config is true without making another if wrapping it?

it's like making a preprocessor #if directive, only not for the preprocessing but for the runtime.

is there such thing? a JIT directive or something like that?

Dimor
  • 53
  • 1
  • 7

2 Answers2

0
if (ConfigurationManager.AppSettings["condition"] == "true" && a)
{
   // do something.
}
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
0

For cleanliness and person preference, I always abstract away the config check into a read-only property:

private bool A
{
    get
    {
        return ConfigurationManager.AppSettings["condition"] == "true" && a;
    }
}

then your statement becomes:

if (A)
{
    //do something.
}

A is a horrible name, but you get the idea.

ConfigurationManager is the recommended API for getting configuration settings, and configuration settings are the recommended way to change runtime behavior.

Aaron Daniels
  • 9,563
  • 6
  • 45
  • 58
  • i'm missed an important detail - this code is part of a method that is fired about 10 times a second. i don't want the if statement to actually run each time the method is fired. is there another way to run a piece of code depending on a config setting value, without having to check it each time? – Dimor Nov 27 '10 at 10:26
  • 1
    What's wrong with checking it every time? To quote MSDN: "For some sections such as appSettings and connectionStrings, use the AppSettings and ConnectionStrings classes. These members perform read-only operations, use a single cached instance of the configuration, and are multithread aware." It's not actually reading from the file every time, if that is what you want to avoid. Checking the in memory configuration cache in an if statement isn't going to cause a performance issue. – Aaron Daniels Nov 28 '10 at 07:35