473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping

Hi I have the following code:

if (chkMaster.Checked)
{
dbFile = dbFile.Substring(0,dbFile.LastIndexOf(@"\")+1) + "master.mdb";
compactDatabase(ref testPassword, dbFile, userID, pword, regPath, systemFile);
if (!testPassword)
{
return;
}
progressBar.Value ++ ;
}

if (chkToolbox.Checked)
{
dbFile = dbFile.Substring(0,dbFile.LastIndexOf(@"\")+1) + "toolbox.mdb";
compactDatabase(ref testPassword, dbFile, userID, pword, regPath, systemFile);
if (!testPassword)
{
return;
}
progressBar.Value ++ ;
}

if (chkImport.Checked)
{
dbFile = dbFile.Substring(0,dbFile.LastIndexOf(@"\")+1) + "Import.mdb";
compactDatabase(ref testPassword, dbFile, userID, pword, regPath, systemFile);
if (!testPassword)
{
return;
}
progressBar.Value ++ ;
}

if (chkUser.Checked)
{
dbFile = dbFile.Substring(0,dbFile.LastIndexOf(@"\")+1) + "User.mdb";
compactDatabase(ref testPassword, dbFile, userID, pword, regPath, systemFile);
if (!testPassword)
{
return;
}
progressBar.Value ++ ;
}

if (chkImpData.Checked)
{
dbFile = dbFile.Substring(0,dbFile.LastIndexOf(@"\")+1) + "ImpData.mdb";
compactDatabase(ref testPassword, dbFile, userID, pword, regPath, systemFile);
if (!testPassword)
{
return;
}
progressBar.Value ++ ;
}
As can be seen the code is quite repetitive, is there some way to reduce
this code by maybe calling a method? CheckBoxes are checked to select which
database to compact. Would it be better to loop throught these somehow rather
than using all the if statements?

Thanks

Nov 16 '05 #1
1 1261
Something like this would already simplify it a lot:

private void CompactDB(string dbFileName)
{
// NEED to check what dbFile is, where it is declared, etc

dbFile = dbFile.Substring(0,dbFile.LastIndexOf(@"\")+1) +
dbFileName;

compactDatabase(ref testPassword, dbFile, userID, pword, regPath,
systemFile);

if (!testPassword)
return;

progressBar.Value++;
}

....
if (chkMaster.Checked)
CompactDB("master.mdb");

if (chkToolbox.Checked)
CompactDB("toolbox.mdb");

if (chkImport.Checked)
CompactDB("Import.mdb");

if (chkUser.Checked)
CompactDB("User.mdb");

if (chkImpData.Checked)
CompactDB("ImpData.mdb");

Now you can of course use a checklist box to put all the checkboxes in
it and loop through it. That would be even better!

--
----
http://michael.moreno.free.fr/

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4038
by: kaptain kernel | last post by:
i've got a while loop thats iterating through a text file and pumping the contents into a database. the file is quite large (over 150mb). the looping causes my CPU load to race up to 100 per...
2
3977
by: ensnare | last post by:
Hi all, I'm using a database session handler and am looking to loop through data residing in the sessions table to make a 'Users online' array. I've found that using urldecode on the data...
2
2555
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"...
45
7395
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
5
2407
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
1
5377
by: Diva | last post by:
Hi, I have a data grid in my application. It has 20 rows and I have set the page size as 5. I have a Submit button on my form and when I click on Submit, I need to loop through the rows in the...
5
3981
by: johnb41 | last post by:
I need to loop through a bunch of textbox controls on my form. The order of the loop is very important. For example, the top one must be read first, then the one below it, etc. My first...
0
1951
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
20
2799
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
2
2061
by: Davaa | last post by:
Dear all, I am a student making a MS Form application in C++. I would ask a question about "Timer". Sample code which I am developing is below. private: System::Void...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.