Reducing Code

G

Guest

Hi, I have the following method which contains some repeated code. Could this
be reduced somehow? Thanks.

private void chooseDatabase()
{
string dbFile = GetPath();
string systemDB = GetSystemDB();
try
{
if (File.Exists(systemDB))
{
string UserID = GetUser();
string Pwd = GetPwd();
progressBar.Value = 1 ;
bool testPassword = true;
if (chkMaster.Checked)
CompactDB(dbFile, systemDB, UserID, Pwd, ref testPassword, "Master.mdb");
if (!testPassword)
return;
if (chkToolbox.Checked)
CompactDB(dbFile, systemDB, UserID, Pwd, ref testPassword, "Toolbox.mdb");
if (!testPassword)
return;
if (chkImport.Checked)
CompactDB(dbFile, systemDB, UserID, Pwd, ref testPassword, "Import.mdb");
if (!testPassword)
return;
if (chkLibrary.Checked)
CompactDB(dbFile, systemDB, UserID, Pwd, ref testPassword, "Library.mde");
if (!testPassword)
return;
if (chkUser.Checked)
CompactDB(dbFile, systemDB, UserID, Pwd, ref testPassword, "User.mdb");
if (!testPassword)
return;
if (chkImpData.Checked)
CompactDB(dbFile, systemDB, UserID, Pwd, ref testPassword, "ImpData.mdb");
if (!testPassword)
return;
progressBar.Value = progressBar.Maximum;
MessageBox.Show("Compacting complete.",messageHeader);
}
else
//Cannot find System database file
{
MessageBox.Show("Cannot find the System database " + systemDB + "\n" + "\n" +
"Check the database name (and path, if specified) to make sure it exists.",
messageHeader,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
progressBar.Value = progressBar.Minimum;
}
}
catch (System.Security.Cryptography.CryptographicException)
{
//System Database User Name corrupt
MessageBox.Show("System database is corrupt." + "\n" + "\n" +
"Replace the file " + systemDB + ".",
messageHeader,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
 
M

Mattias Sjögren

Hi, I have the following method which contains some repeated code. Could this
be reduced somehow?

CheckBox[] checkBoxes = {chkMaster, chkToolbox, chkImport, chkLibrary,
chkUser, chkImpData};
string[] databases = {"Master.mdb", "Toolbox.mdb", "Import.mdb",
"Library.mdb", "User.mdb", "ImpData.mdb"};

....

for ( int i = 0; i < checkBoxes.Length; i++ ) {
if ( checkBoxes.Checked )
CompactDB(dbFile, systemDB, UserID, Pwd, ref testPassword,
databases);
if (!testPassword)
return;
}



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top