Skip to content

Looking for a cleaner way to do this.

Bob Boursaw

Asked May 24, 2007, 9:33 AM EDT1 answer
Share
LinkedInX
Good afternoon,

My code is working but I have to believe there are far better ways to get the job done thatn how I've done it.  Basically, I'm using an app.config file to store and retrieve application settings from.  I am wondering if there is a cleaner way to format / write the code without having a tone if if / else statements.

The goal here is to check the app.config file during the form load and then if the value parameters say "configure" I will then execute some code to walk the user through configuring the application.

Or, (as I'm very new to this stuff), is what I have fine and I should leave it alone?

Thanks,
Bob.

--- my code ---

C#
private void PERMain_Load(object sender, System.EventArgs e)
{
// Set desktop configuration parameters via the app.config file (if exist)
if (ConfigurationSettings.AppSettings["DThost"] != "configure")
{
ADSHost = ConfigurationSettings.AppSettings["DThost"];
if (ConfigurationSettings.AppSettings["DTport"] != "configure")
{
ADSPort = ConfigurationSettings.AppSettings["DTport"];
if (ConfigurationSettings.AppSettings["PERuserID"] != "configure")
{
agentID = Convert.ToInt32(ConfigurationSettings.AppSettings["PERuserID"]);
if (ConfigurationSettings.AppSettings["extension"] != "configure")
{
extension = Convert.ToInt32(ConfigurationSettings.AppSettings["extension"]);
if (ConfigurationSettings.AppSettings["instrument"] != "configure")
{
instrument = Convert.ToInt16(ConfigurationSettings.AppSettings["instrument"]);
if (ConfigurationSettings.AppSettings["password"] != "configure")
{
password = ConfigurationSettings.AppSettings["password"];

if (ConfigurationSettings.AppSettings["macroName"] != "configure")
{
macroName = ConfigurationSettings.AppSettings["macroName"];
if (ConfigurationSettings.AppSettings["fieldName"] != "configure")
{
fieldName = ConfigurationSettings.AppSettings["fieldName"];
if (ConfigurationSettings.AppSettings["PERmaxLogFiles"] != "configure")
{
maxLogFiles = Convert.ToInt16(ConfigurationSettings.AppSettings["PERmaxLogFiles"]);
if (ConfigurationSettings.AppSettings["PERloggingLevel"] != "configure")
{
loggingLevel = Convert.ToInt16(ConfigurationSettings.AppSettings["PERloggingLevel"]);
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}
else
{
MessageBox.Show("Please configure the desktop application.");
Application.Exit();
}
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    I think a more organized approach to doing this is to just leave the setting out if you are not using, rather than mixing a configure variable with the meaning of your app setting.  Then you don't need all these if statements seeing if it's configured.  You can also just use the appsettings directly in your code rather than checking and assigning them all.  When you need to know the port value just check

    if (Configuration.AppSettings["myport"] != null)
      {
              // do whatever with  (Configuration.AppSettings["myport"] )
      }
    0