Is it a bad design to pass a form as an argument to a class in order to access some of its variables or methods?

I found that I can either pass 8 arguments to the constructor of the class, or simply pass the form variable instead.

However, since I am not using everything in form, it looks like this might be a bad design?

Also, for the objects I am accessing, I will need to provide Accessors for.

Does it violate the principles of OOP?

+5
source share
5 answers

It depends - if you use the form as a specific type of form and “logically”, it makes sense that you work with the form and then pass the link to the form.

. "", :

void DoSomething(Employee employee) { ...

:

void DoSomething(string firstName, string lastName, DateTime hireDate...) { ...

.

, , , , , , .

, , , Accessors .

, , , , , ... , , .

+5

gui gui , , /, , .

. . "" gui .

, 8 , 8 ? , 8 -. , , gui .

+3

, , , 8 , . , ( - ), , .

- , , , , SRP, .

+2

, , , :

class MainForm : Form
{
    // stuff
}

class ChildForm : Form
{
    private MainForm _mainFrm;
    public ChildForm( MainForm frm )
    {
        _mainFrm = frm;
    }

    private void someButton_Click( ... )
    {
        _mainFrm.UpdateSomeText();
    }
}

. , , , , , , , , . , ChildForm MainForm, , , .

, , . , , . , , , , , - .

+1

, (, , ..), Form . , THAT OO. - , , .

Even if you need customer details in a new class, and you feel like transferring a CustomerForm that contains all the necessary information, DO NOT DO IT. Create a client class, specify an instance of this class from the form, and transfer this instance to a new class. If you ever change the user interface, or if you ever need to automate the part of the workflow that was used as a guide, you'll be glad you did.

0
source

All Articles