I am trying to learn the proper way to program C# for Windows applications, and I have a question about the correct or best way to pass information between the main form and the classes that the form contains.
For example, let us say my form "Form1" contains a class "MyClass" that does most of the calculations, etc. The main form creates an instance of the MyClass class. Let us say, for example, that the user clicks a button on the form that then calls a function "MyFunction" contained in MyClass. The function MyFunction performs some tasks, and then based on the outcome of these tasks, then needs to update or change various controls located on the main form.
What is the best or proper way to do this? Should I send a reference to Form1 as an argument in the call to the MyClass function MyFunction, and then reference the Form1 controls from MyFunction directly? Or should I just set some public informational variables within MyFunction, then return from this function without doing anything to the Form1 controls, and then let the "on event" function in the main form which called MyFunction examine these public variables and then do the updates or changes to its controls?
I am wondering what is best from coding correctness, efficiency, and - very important also - understandability of the code to myself and future programmers.
Thanks for any advice!
Posted Jul 10, 2008, 11:01 AM
stevePosted Jul 10, 2008, 10:38 AM
Hi and thanks for the answer.
Yes, the way you describe is how I have started to do things with my first C# project.
It has been extremely tempting to let my "MyClass" do the work on the main form directly. For example, the user enters the name of an entity in a database, and this name is passed to a function in MyClass that gets the entity, and then produces an image to be displayed on the main form. The size of the image on the main form depends on which entity is retrieved from the database, and I want to move other controls around based on this size. So it was tempting to manipulate the form controls directly from the function in MyClass rather than let the function in MyClass return, and then call functions in the Form1 class to manipulate the Form1 controls.
But I can see the point, that it is far better from code organization (and for a more linear call stack) to do things as you describe.
Thanks!
Posted Jul 10, 2008, 9:31 AM
User clicks the "Calculate" button.
Two numbers are passed to MyClass.MyFunction as parameters
MyFunction processes these numbers as necessary
MyFunction returns the results of the calculation
The Main Form is still waiting on a return results from MyFunction and receives it
The Main Form updates its user interface appropriately, displaying the result of the calculation
If you understand how the call stack works, it would make more sense. Example:
Button_Click Eventhandler Method
Button_Click Eventhandler Method calls MyClass.MyFunction
MyClass.MyFunction does some calculations
MyClass.MyFunction returns a result and its stack frame is removed from the calls stack
Button_Click Eventhandler Method gets result from MyClass.MyFunction
Button_Click Eventhandler Method updates user interface (or better, calls another method to do it, passing the new calculation as a parameter).
Check out Functional Decomposition to get a better idea