0

I am very new to C# and have the following snippet of repeated code from one monstrosity of a program:

    private void subjectBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] igcseSubjects = new string[5] {"IGCSE Maths", "IGCSE English", "IGCSE Chem", "IGCSE Phys", "IGCSE Bio"};
        string selectedSubject = (string) subjectBox1.SelectedItem;
        if (igcseSubjects.Contains(selectedSubject))
        {
            nBox1.Visible = aBox1.Visible = mBox1.Visible = eBox1.Visible = false;
            cBox1.Visible = true;
            maxBox1.Text = "100";
        }
        else
        {
            nBox1.Visible = aBox1.Visible = mBox1.Visible = eBox1.Visible = true;
            cBox1.Visible = false;
            maxBox1.Text = "20";
        }

    }

    private void subjectBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] igcseSubjects = new string[5] { "IGCSE Maths", "IGCSE English", "IGCSE Chem", "IGCSE Phys", "IGCSE Bio" };
        string selectedSubject = (string)subjectBox2.SelectedItem;
        if (igcseSubjects.Contains(selectedSubject))
        {
            nBox2.Visible = aBox2.Visible = mBox2.Visible = eBox2.Visible = false;
            cBox2.Visible = true;
            maxBox2.Text = "100";
        }
        else
        {
            nBox2.Visible = aBox2.Visible = mBox2.Visible = eBox2.Visible = true;
            cBox2.Visible = false;
            maxBox2.Text = "20";
        }
    }

As you can see, the two event handlers are exact replicas, minus the controller variable names. The actual program has many more event handlers just like these, and is getting out of hand.

I am 99% sure there is a way to achieve the same outcome without all this copy/pasting, but can't figure out how to get around the different variable names -- Can I use one method?

How can I go about refactoring this code to not be so repetitive?

1
  • Is the only difference the nBox and cBox control references? Commented Jun 27, 2016 at 7:36

1 Answer 1

3

Basically, you can use a single event handler implementation method for all the SubjectBox controls. Based upon who sent the event, you can use decide which other controls to use for your logic.

A few remarks if this is the case: It feels like your form might be to complex, or you could introduce a UserControl exposing properties for all the boxes.

    private void SubjectBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SubjectBox_SelectedIndexChangedImpl((SomeBox)sender, aBox1, nBox1, cBox1, mBox1, maxBox1);
    }

    private void SubjectBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        SubjectBox_SelectedIndexChangedImpl((SomeBox)sender, aBox2, nBox2, cBox2, mBox2, maxBox2);
    }

    private void SubjectBox_SelectedIndexChangedImpl(SomeBox sender, SomeBox aBox, SomeBox nBox, SomeBox cBox, ......)
    {
        string[] igcseSubjects = new string[5] { "IGCSE Maths", "IGCSE English", "IGCSE Chem", "IGCSE Phys", "IGCSE Bio" };
        string selectedSubject = (string)subjectBox.SelectedItem;
        if (igcseSubjects.Contains(selectedSubject))
        {
            nBox.Visible = aBox.Visible = mBox.Visible = eBox.Visible = false;
            cBox.Visible = true;
            maxBox.Text = "100";
        }
        else
        {
            nBox.Visible = aBox.Visible = mBox.Visible = eBox.Visible = true;
            cBox.Visible = false;
            maxBox.Text = "20";
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Personally I think that this solution will scale very badly. When this will be called by 15 controls the if list will be impressive.
I think I'll go with your suggestion here - there are 8 controls that need event handlers like this. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.