1
Answer

Looking for a cleaner way to do this.

Bob Boursaw

Bob Boursaw

17y
1.5k
1
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 ---

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();

}

}

Answers (1)