Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Application settings in constructor

Application settings in constructor

Scheduled Pinned Locked Moved ASP.NET
question
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mail572352
    wrote on last edited by
    #1

    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? :)

    K B T 3 Replies Last reply
    0
    • M mail572352

      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? :)

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M mail572352

        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? :)

        B Offline
        B Offline
        badgrs
        wrote on last edited by
        #3
        string mySetting = ConfigurationManager.AppSettings["MySetting"]);
        if(mySetting != null)
        {
        _MySetting = mySetting;
        }
        else
        {
        _MySetting = "default value";
        }
        

        Theres one way.

        1 Reply Last reply
        0
        • M mail572352

          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? :)

          T Offline
          T Offline
          thowra
          wrote on last edited by
          #4

          How about this:

          _MySetting = (ConfigurationManager.AppSettings["MySetting "] == null) ? "default value" : ConfigurationManager.AppSettings["MySetting"]);

          Evil cannot be conquered in the world... It can only be resisted within oneself.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups