samedi 25 avril 2015

Is there an easier way of writing several if/else statements into eachother?

I sometimes have myself writing code which requires several layers of if/else statements incorporated into each other (example can be found below).
I was wondering if I could shorten it up a bit, because sometimes I have trees of if/else statements of over 70 lines of code, and they are honestly just filling way too much compared to how many of the lines seem redundant.

Here's an example code:

if (labelGiveTip1.Visible == true)
        {
            if (labelGiveTip2.Visible == true)
            {
                labelGiveTip3.Visible = true;
                if (labelGiveTip3.Visible == true)
                {
                    Custom_DialogBox.Show("All of your hints for this assignment is used, do you want annother assignmment?", //main text argument
                        "Error: No more hints",  //header argument
                        "Back",                  //first button text argument
                        "Get a new assignment"); //second button text argument
                                                 //this is a custom dialog box



                    result = Custom_DialogBox.result; 
                    switch (result) 
                    {
                        case DialogResult.Yes:
                            {
                                buttonNextAssignment.PerformClick(); 
                                break;
                            }
                        case DialogResult.Cancel:
                            {
                                break; 
                            }
                        default:
                            {
                                break;
                            }
                    }
                }
            }
            else
            {
                labelGiveTip2.Visible = true;
            }
        }
        else
        {
            labelGiveTip1.Visible = true;
        }

Aucun commentaire:

Enregistrer un commentaire