Skip to main content
added 10 characters in body
Source Link
SirPython
  • 13.5k
  • 3
  • 38
  • 93

I have a block of code below. The allDone() method at the bottom should only be run if the allCompleted == true. It should run through each of the statements to test.

  • allCompleted >: This starts as true so the below logic works right.

  • run*.Checked >: This is based on a check box in a form. This block should only run if this box is checked.

  • cmd >: This is a generic string variable stating whether another part of the code (not shown here) was run successfully. If it has run successfully this string will read "done".

So afterAfter those options, if all enabled (run*.Checked == true) methods have returned the cmd* string as "done""done" (everything thatsthat's checked has run successfully) then allCompleted should be == true at the end so allDone() gets run. 

If one single enabled method returns false false (there was an error somewhere or otherwise it did not return "done""done"), then the allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statement statement.

bool allCompleted = true;

if (runPart1.Checked)
    if (cmdPart1 == "done")
        allCompleted = ((allCompleted)? true : false);
    else
        allCompleted = false;

if (runPart2.Checked)
    if (cmdPart2 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;

if (runPart3.Checked)
    if (cmdPart3 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;

if (runPart4.Checked)
    if (cmdPart4 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;
    

if (allCompleted)
    allDone();

So if at anytime one of the enabled parts fail the code will basically just move on. As

As it stands this code works, I just feel like it could be written better. Is this the best way or have I got it? Something about it makes me feel awkward still.

EDIT: Also, each time one of the parts completes, it runs this method, so it will run a few times being false in the end until the last one runs and all the others are "done" in which case it should completes and run allDone().

I have a block of code below. The allDone() method at the bottom should only be run if the allCompleted == true. It should run through each of the statements to test.

  • allCompleted > This starts as true so the below logic works right

  • run*.Checked > This is based on a check box in a form. This block should only run if this box is checked

  • cmd > This is a generic string variable stating whether another part of the code (not shown here) was run successfully. If it has run successfully this string will read "done"

So after those options, if all enabled (run*.Checked == true) methods have returned the cmd* string as "done" (everything thats checked has run successfully) then allCompleted should == true at the end so allDone() gets run. If one single enabled method returns false (there was an error somewhere or otherwise it did not return "done") then the allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statement.

bool allCompleted = true;

if (runPart1.Checked)
    if (cmdPart1 == "done")
        allCompleted = ((allCompleted)? true : false);
    else
        allCompleted = false;

if (runPart2.Checked)
    if (cmdPart2 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;

if (runPart3.Checked)
    if (cmdPart3 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;

if (runPart4.Checked)
    if (cmdPart4 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;
    

if (allCompleted)
    allDone();

So if at anytime one of the enabled parts fail the code will basically just move on. As it stands this code works, I just feel like it could be written better. Is this the best way or have I got it? Something about it makes me feel awkward still.

EDIT: Also, each time one of the parts completes, it runs this method, so it will run a few times being false in the end until the last one runs and all the others are "done" in which case it should completes and run allDone().

I have a block of code below. The allDone() method at the bottom should only be run if the allCompleted == true. It should run through each of the statements to test.

  • allCompleted: This starts as true so the below logic works right.

  • run*.Checked: This is based on a check box in a form. This block should only run if this box is checked.

  • cmd: This is a generic string variable stating whether another part of the code (not shown here) was run successfully. If it has run successfully this string will read "done".

After those options, if all enabled (run*.Checked == true) methods have returned the cmd* string as "done" (everything that's checked has run successfully) then allCompleted should be true at the end so allDone() gets run. 

If one single enabled method returns false (there was an error somewhere or otherwise it did not return "done"), then the allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statement.

bool allCompleted = true;

if (runPart1.Checked)
    if (cmdPart1 == "done")
        allCompleted = ((allCompleted)? true : false);
    else
        allCompleted = false;

if (runPart2.Checked)
    if (cmdPart2 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;

if (runPart3.Checked)
    if (cmdPart3 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;

if (runPart4.Checked)
    if (cmdPart4 == "done")
        allCompleted = ((allCompleted) ? true : false);
    else
        allCompleted = false;
    

if (allCompleted)
    allDone();

So if at anytime one of the enabled parts fail the code will basically just move on.

As it stands this code works, I just feel like it could be written better. Is this the best way or have I got it? Something about it makes me feel awkward still.

EDIT: Also, each time one of the parts completes, it runs this method, so it will run a few times being false in the end until the last one runs and all the others are "done" in which case it should completes and run allDone().

This "answer" information doesn't need to be appended to the question
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Nested Ifif statements with 3 different Parameters. Can this be written better?parameters

allCompleted > this starts as true so the below logic works right

run.Checked* > This is based on a check box in a form. This block should only run if this box is checked

cmd* > This is a generic string variable stating whether another part of the code (not shown here) was run successfully. If it has run successfully this string will read "done"

  • allCompleted > This starts as true so the below logic works right

  • run*.Checked > This is based on a check box in a form. This block should only run if this box is checked

  • cmd > This is a generic string variable stating whether another part of the code (not shown here) was run successfully. If it has run successfully this string will read "done"

So after those options, if all enabled (run*.Checked == true) methods have returned the cmd* string as "done" (everything thats checked has run successfully) then allCompleted should == true at the end so allDone() gets run. If one single enabled method returns false (there was an error somewhere or otherwise it did not return "done") then the allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statement. The Code:

EDIT: Also, each time one of the parts completes, it runs this method., so it will run a few times being false in the end until the last one runs and all the others are "done" in which case it should completes and run allDone()

ANSWER: I am marking @Mark Loeser's answer as the answer. This answer is the simplest and makes it easy to add more parts (which more will be added periodically, about 10 in total). Although, Later on I may need to get a little frisky with my set up where I will need a more complex and intuitive solution, in which case I will probably use @Michaels answer, which I like. Also keep in mind that even though the answer isn't voted the highest, because as @Anthony Pegram pointed out, part of the original code was flawed to begin with. I appreciate pointing that out (along with a few other tidbits in the comments) and I think this whole question has made me learn a few new tricks as well. Im relatively new to C# and haven't been coding for a long period of time to begin with. I appreciate all the feed back very muchallDone().

Nested If statements with 3 different Parameters. Can this be written better?

allCompleted > this starts as true so the below logic works right

run.Checked* > This is based on a check box in a form. This block should only run if this box is checked

cmd* > This is a generic string variable stating whether another part of the code (not shown here) was run successfully. If it has run successfully this string will read "done"

So after those options, if all enabled (run*.Checked == true) methods have returned the cmd* string as "done" (everything thats checked has run successfully) then allCompleted should == true at the end so allDone() gets run. If one single enabled method returns false (there was an error somewhere or otherwise it did not return "done") then the allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statement. The Code:

EDIT: Also, each time one of the parts completes, it runs this method. so it will run a few times being false in the end until the last one runs and all the others are "done" in which case it should completes and run allDone()

ANSWER: I am marking @Mark Loeser's answer as the answer. This answer is the simplest and makes it easy to add more parts (which more will be added periodically, about 10 in total). Although, Later on I may need to get a little frisky with my set up where I will need a more complex and intuitive solution, in which case I will probably use @Michaels answer, which I like. Also keep in mind that even though the answer isn't voted the highest, because as @Anthony Pegram pointed out, part of the original code was flawed to begin with. I appreciate pointing that out (along with a few other tidbits in the comments) and I think this whole question has made me learn a few new tricks as well. Im relatively new to C# and haven't been coding for a long period of time to begin with. I appreciate all the feed back very much.

Nested if statements with 3 different parameters

  • allCompleted > This starts as true so the below logic works right

  • run*.Checked > This is based on a check box in a form. This block should only run if this box is checked

  • cmd > This is a generic string variable stating whether another part of the code (not shown here) was run successfully. If it has run successfully this string will read "done"

So after those options, if all enabled (run*.Checked == true) methods have returned the cmd* string as "done" (everything thats checked has run successfully) then allCompleted should == true at the end so allDone() gets run. If one single enabled method returns false (there was an error somewhere or otherwise it did not return "done") then the allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statement.

EDIT: Also, each time one of the parts completes, it runs this method, so it will run a few times being false in the end until the last one runs and all the others are "done" in which case it should completes and run allDone().

changed some formatting and punctuation
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188

I have a block of code below. The allDone()allDone() method at the bottomebottom should only be run if the allCompleted == trueallCompleted == true. It should run through each of the statements to test.

So after those options, if all enabled (run*.Checked == truerun*.Checked == true) methods have returned the cmd*cmd* string as "done" (everything thats checked has run successfully) then allCompletedallCompleted should == true== true at the end so allDone()allDone() gets run. If one single enabled method returns false (there was an error somewhere or otherwise it did not return "done") then the allDone()allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statementif (allCompleted) statement. The Code:

ANSWER: I am marking @Mark Loeser's answer as the answer. This answer is the simplest and makes it easy to add more parts (which more will be added periodically, about 10 in total). Although, Later on I may need to get a little frisky with my setupset up where I will need a more complex and intuitive solution, in which case I will probably use @Michaels answer, which I like. Also keep in mind that even though the answer isntisn't voted the highest, because as @Anthony Pegram pointed out, part of the original code was flawed to begin with. I appreciate pointing that out (along with a few other tidbits in the comments) and I think this whole question has made me learn a few new tricks as well. Im relatively new to C# and haventhaven't been coding for a long period of time to begin with. I appreciate all the feed back very much. Thanks.

I have a block of code below. The allDone() method at the bottome should only be run if the allCompleted == true. It should run through each of the statements to test.

So after those options, if all enabled (run*.Checked == true) methods have returned the cmd* string as "done" (everything thats checked has run successfully) then allCompleted should == true at the end so allDone() gets run. If one single enabled method returns false (there was an error somewhere or otherwise it did not return "done") then the allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statement. The Code:

ANSWER: I am marking @Mark Loeser's answer as the answer. This answer is the simplest and makes it easy to add more parts (which more will be added periodically, about 10 in total). Although, Later on I may need to get a little frisky with my setup where I will need a more complex and intuitive solution, in which case I will probably use @Michaels answer, which I like. Also keep in mind that even though the answer isnt voted the highest, because as @Anthony Pegram pointed out, part of the original code was flawed to begin with. I appreciate pointing that out (along with a few other tidbits in the comments) and I think this whole question has made me learn a few new tricks as well. Im relatively new to C# and havent been coding for a long period of time to begin with. I appreciate all the feed back very much. Thanks.

I have a block of code below. The allDone() method at the bottom should only be run if the allCompleted == true. It should run through each of the statements to test.

So after those options, if all enabled (run*.Checked == true) methods have returned the cmd* string as "done" (everything thats checked has run successfully) then allCompleted should == true at the end so allDone() gets run. If one single enabled method returns false (there was an error somewhere or otherwise it did not return "done") then the allDone() method should not be run and the code will continue, skipping the last if (allCompleted) statement. The Code:

ANSWER: I am marking @Mark Loeser's answer as the answer. This answer is the simplest and makes it easy to add more parts (which more will be added periodically, about 10 in total). Although, Later on I may need to get a little frisky with my set up where I will need a more complex and intuitive solution, in which case I will probably use @Michaels answer, which I like. Also keep in mind that even though the answer isn't voted the highest, because as @Anthony Pegram pointed out, part of the original code was flawed to begin with. I appreciate pointing that out (along with a few other tidbits in the comments) and I think this whole question has made me learn a few new tricks as well. Im relatively new to C# and haven't been coding for a long period of time to begin with. I appreciate all the feed back very much.

added answer edit/summary
Source Link
Loading
Tweeted twitter.com/#!/StackCodeReview/status/33311897138757632
added 230 characters in body
Source Link
Loading
Source Link
Loading