3
Answers

Would like advice on how best to delegate tasks between the main form and its classes.

Ask a question
steve

steve

16y
2.7k
1

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!


Answers (3)