Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

I found that I can either pass 8 arguments to a class constructor or just pass the form variable instead.

However, since I am not using everything on the form it seems like it may be bad design?

Also, the objects I do access I would need to provide accessors for.

Does it violate the principles of OOP?

like image 405
Chris Smith Avatar asked Sep 13 '11 02:09

Chris Smith


People also ask

How do you pass a value from one method to another in Java?

We can't directly pass the whole method as an argument to another method. Instead, we can call the method from the argument of another method. // pass method2 as argument to method1 public void method1(method2()); Here, the returned value from method2() is assigned as an argument to method1() .

When invoking a method with an object argument what is passed?

When invoking a method with an object argument, the reference to the object is passed. You must pass in an object for a method to be able to modify an argument. In Java, objects are also passed by value; however, an object's value is a reference.

Can a class take arguments C#?

In C#, arguments can be passed to parameters either by value or by reference. Remember that C# types can be either reference types ( class ) or value types ( struct ):


3 Answers

It depends - if you're using the form as that specific type of form, and "logically" it makes sense that you're working with the form, then by all means, pass a reference to the form.

It's just like any other class - If I was going to be accessing elements of an "employee", I'd write:

void DoSomething(Employee employee) { ...

Instead of:

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

The first is very clean and obvious.

However, if the data you're using is unrelated to the form, it'd be better to encapsulate it into its own class usable by both the form and your class.

Also, the objects I do access I would need to provide accessors for.

If this is the case, I suspect that having a class encapsulating the data is likely a better design... The form could expose a property or method that returns an instance of that class, and pass it into your second class.

like image 82
Reed Copsey Avatar answered Nov 08 '22 03:11

Reed Copsey


Passing a gui form to either other gui components or even worse, a model/library that does work does break encapsulation and creates a tight coupling.

The form should abstract the data and the model below. Other model or library classes should be passed model objects. A typical pattern is to "bind" the gui layer to the model.

Instead of passing 8 variables, do the 8 variables logically break into different objects? Ideally, you would pass an object or set of objects which may collectively contain 8 member variables. Then you can simply pass references to objects that are contained in the same model that your gui is abstracting and bound to.

like image 45
bryanmac Avatar answered Nov 08 '22 05:11

bryanmac


Without seeing the class, I can almost guarantee the class taking 8 arguments is violating the Single Responsibility Principle. It could be a class generated to represent a table in a database (or something to that effect) in which case you should encapsulate it in its own class as pass it around instead of the form.

Something else to consider is that the form you're reviewing is also violating SRP since it's both displaying data and being used as backing for another form.

like image 37
Austin Salonen Avatar answered Nov 08 '22 04:11

Austin Salonen