Application settings in constructor
-
Hi, How best to get application settings to initialize a property or member variable? I have this in my constructor:
_MySetting = ConfigurationManager.AppSettings["MySetting"]);
This might fail if the user hasn't got the application setting defined. Constructor should only initialize members really so how best to write it in a constructor so that it initializes to a defined setting or another value if the setting is not defined? :)
-
Hi, How best to get application settings to initialize a property or member variable? I have this in my constructor:
_MySetting = ConfigurationManager.AppSettings["MySetting"]);
This might fail if the user hasn't got the application setting defined. Constructor should only initialize members really so how best to write it in a constructor so that it initializes to a defined setting or another value if the setting is not defined? :)
If you happen to be using .net 2.0 you can use the ?? So your code would be: _MySetting = ConfigurationManager.AppSettings["MySetting"]) ?? "DefaultValueHere"; If you are using .net 1.1 you would probably have to do something like: if (ConfigurationManager.AppSettings["MySetting"] == null) { _MySetting = "DefaultValueHere"; } else { _MySetting = ConfigurationManager.AppSettings["MySetting"]); } Hope that helps. Ben
-
Hi, How best to get application settings to initialize a property or member variable? I have this in my constructor:
_MySetting = ConfigurationManager.AppSettings["MySetting"]);
This might fail if the user hasn't got the application setting defined. Constructor should only initialize members really so how best to write it in a constructor so that it initializes to a defined setting or another value if the setting is not defined? :)
-
Hi, How best to get application settings to initialize a property or member variable? I have this in my constructor:
_MySetting = ConfigurationManager.AppSettings["MySetting"]);
This might fail if the user hasn't got the application setting defined. Constructor should only initialize members really so how best to write it in a constructor so that it initializes to a defined setting or another value if the setting is not defined? :)