Coded by an Expectional person
-
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.
-
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.
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?
-
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?
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"]
orEnableLogging = ConfigurationSettings.AppSettings["EnableLogging"] ?? string.Empty;
-
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"]
orEnableLogging = ConfigurationSettings.AppSettings["EnableLogging"] ?? string.Empty;
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 ofConfigurationManager.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.