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. Other Discussions
  3. The Weird and The Wonderful
  4. Coded by an Expectional person

Coded by an Expectional person

Scheduled Pinned Locked Moved The Weird and The Wonderful
4 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    senny
    wrote on last edited by
    #1
    string EnableLogging = string.Empty;
    string WSURL  = string.Empty;
    string TransformXSLPath = string.Empty;
    ....
    try
    {
        EnableLogging = ConfigurationSettings.AppSettings["EnableLogging"].ToString();
    }
    catch( Exception ex )
    {
        EnableLogging = string.Empty;
    }
    
    try
    {
        WSURL = ConfigurationSettings.AppSettings["WSURL"].ToString();
    }
    catch( Exception ex )
    {
        WSURL = string.Empty;
    }
    try
    {
        TransformXSLPath = ConfigurationSettings.AppSettings["TransformXSLPath"].ToString();
    }
    catch( Exception ex )
    {
        TransformXSLPath = string.Empty;
    }
    ....
    

    like this for all variables that are read from config file.

    A 1 Reply Last reply
    0
    • S senny
      string EnableLogging = string.Empty;
      string WSURL  = string.Empty;
      string TransformXSLPath = string.Empty;
      ....
      try
      {
          EnableLogging = ConfigurationSettings.AppSettings["EnableLogging"].ToString();
      }
      catch( Exception ex )
      {
          EnableLogging = string.Empty;
      }
      
      try
      {
          WSURL = ConfigurationSettings.AppSettings["WSURL"].ToString();
      }
      catch( Exception ex )
      {
          WSURL = string.Empty;
      }
      try
      {
          TransformXSLPath = ConfigurationSettings.AppSettings["TransformXSLPath"].ToString();
      }
      catch( Exception ex )
      {
          TransformXSLPath = string.Empty;
      }
      ....
      

      like this for all variables that are read from config file.

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #2

      Assuming this is C#... exception handling is probably not necessary (though better safe than sorry), ToString is not necessary on something that is already a string, the initial assignments are not necessary if they're going to get assigned in the catches, catching a particular exception is not necessary if the exception variable is not going to be used (there are some nuances to that, but for this example it seems unnecessary), and the repeated functionality is not encapsulated in a function. Maybe this would be more appropriate:

      private string GetConfigSetting(string key)
      {
      string val;
      try
      {
      val = ConfigurationSettings.AppSettings[key];
      }
      catch
      {
      val = string.Empty;
      }
      return val;
      }

      // In a land far far away...
      string EnableLogging = GetConfigSetting("EnableLogging");
      string WSURL = GetConfigSetting("WSURL");
      string TransformXSLPath = GetConfigSetting("TransformXSLPath");

      Does that about do it, or was there something else you noticed that was horrific/shameful?

      [WikiLeaks Cablegate Cables]

      S 1 Reply Last reply
      0
      • A AspDotNetDev

        Assuming this is C#... exception handling is probably not necessary (though better safe than sorry), ToString is not necessary on something that is already a string, the initial assignments are not necessary if they're going to get assigned in the catches, catching a particular exception is not necessary if the exception variable is not going to be used (there are some nuances to that, but for this example it seems unnecessary), and the repeated functionality is not encapsulated in a function. Maybe this would be more appropriate:

        private string GetConfigSetting(string key)
        {
        string val;
        try
        {
        val = ConfigurationSettings.AppSettings[key];
        }
        catch
        {
        val = string.Empty;
        }
        return val;
        }

        // In a land far far away...
        string EnableLogging = GetConfigSetting("EnableLogging");
        string WSURL = GetConfigSetting("WSURL");
        string TransformXSLPath = GetConfigSetting("TransformXSLPath");

        Does that about do it, or was there something else you noticed that was horrific/shameful?

        [WikiLeaks Cablegate Cables]

        S Offline
        S Offline
        senny
        wrote on last edited by
        #3

        Agreed, no need of .ToString() as AppSettings[] will return string only. But this line
        EnableLogging = ConfigurationSettings.AppSettings["EnableLogging"].ToString();
        will throw only Objectrefnotset exception since .ToString() is used and if the key is not found in the config file. I dont find a reason for any exception. This should be the right way
        if (ConfigurationSettings.AppSettings["EnableLogging"] != null ) EnableLogging = ConfigurationSettings.AppSettings["EnableLogging"] or EnableLogging = ConfigurationSettings.AppSettings["EnableLogging"] ?? string.Empty;

        A 1 Reply Last reply
        0
        • S senny

          Agreed, no need of .ToString() as AppSettings[] will return string only. But this line
          EnableLogging = ConfigurationSettings.AppSettings["EnableLogging"].ToString();
          will throw only Objectrefnotset exception since .ToString() is used and if the key is not found in the config file. I dont find a reason for any exception. This should be the right way
          if (ConfigurationSettings.AppSettings["EnableLogging"] != null ) EnableLogging = ConfigurationSettings.AppSettings["EnableLogging"] or EnableLogging = ConfigurationSettings.AppSettings["EnableLogging"] ?? string.Empty;

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #4

          Yeah, I can't get it to throw an exception (even when I use an invalid key), though the MSDN documentation says an exception can be thrown. One more thing I noticed is that ConfigurationSettings.AppSettings is deprecated in favor of ConfigurationManager.AppSettings, though it may be fine if the code resides in an older version of the .Net Framework in which that was not yet deprecated.

          [WikiLeaks Cablegate Cables]

          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