简体   繁体   中英

How can I make overload simpler

I have a helper class that handles some methods for a toolbar. Now I have created 5 constructor with different overloads. My question is, how can I make this contructor overload simpler? The way I am doing it works, but in case I will need to implement fe 10 RichTextBoxes, I don't want to create an overload for every possible RichTextBox and handle everyone in the methods. I'm convinced there is a simpler way but one way or another, I can see to figure this out.

I tried to make a List but get errors in return that I can't make a list of a namespace which is logic.

    public class RichtTextBoxHelper
    {
    private RichTextBox _textBox;
    private RichTextBox _textbox2;
    private RichTextBox _textbox3;
    private RichTextBox _textbox4;
    private RichTextBox _textbox5;

    public RichtTextBoxHelper(RichTextBox textBox)
    {
        _textBox = textBox;
    }
    public RichtTextBoxHelper(RichTextBox textBox, RichTextBox textbox2)
    {
        _textBox = textBox;
        _textbox2 = textbox2;
    }

    public RichtTextBoxHelper(RichTextBox textBox, RichTextBox textbox2, RichTextBox textbox3)
    {
        _textBox = textBox;
        _textbox2 = textbox2;
        _textbox3 = textbox3;
    }

    public RichtTextBoxHelper(RichTextBox textBox, RichTextBox textbox2, RichTextBox textbox3, RichTextBox textbox4)
    {
        _textBox = textBox;
        _textbox2 = textbox2;
        _textbox3 = textbox3;
        _textbox4 = textbox4;
    }

    public RichtTextBoxHelper(RichTextBox textBox, RichTextBox textbox2, RichTextBox textbox3, RichTextBox textbox4, RichTextBox textbox5)
    {
        _textBox = textBox;
        _textbox2 = textbox2;
        _textbox3 = textbox3;
        _textbox4 = textbox4;
        _textbox5 = textbox5;
    }

    public void CutClick()
    {
        _textBox.Cut();
        _textbox2.Cut();
        _textbox3.Cut();
        _textbox4.Cut();
        _textbox5.Cut();
    }

Various methods like the cut one.

Is there an easier, more clean way to do this?

You should use an array of RichTextBox s:

private RichTextBox[] textBoxes;

Now you only need one constructor:

public RichtTextBoxHelper(params RichTextBox[] textBoxes) {
    this.textBoxes = textBoxes ?? throw new ArgumentNullException(nameof(textBoxes));
}

If there is a maximum number of text boxes that you can handle, just make a check:

public RichtTextBoxHelper(params RichTextBox[] textBoxes) {
    if (textBoxes is null)
    {
        throw new ArgumentNullException(nameof(textBoxes));
    }

    if (textBoxes.Length > maxTextBoxes) :
        throw new ArgumentException("Too many text boxes!", nameof(textBoxes))
    }
    this.textBoxes = textBoxes;
}

The CutClick method can be simply:

public void CutClick() {
    foreach(var textBox in textBoxes) {
        textBox.Cut();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM