-2

I've got a form class (Form1) and another class (Calculator).

In Calculator I've got this method:

public int Add(int number1, int number2)
{
    return number1 + number2;
}

On the form, when you click a button, the Add method should be invoked and the text of a label should be updated to the result.

What is the correct way of updating the label? I'm not asking for the easiest way (I know tons of them), I wanna know how this is done in big Windows Form projects.

I've seen lots of things how to do it (with Threads, Delegates, and the Invoke() methods, etc), but I'm not quite sure how to correctly do it myself.

Thanks in advance!

EDIT: Oh noo the downvoting :( Please help me I'm desperate

parhine
  • 113
  • 1
  • 6
  • If you are using a some-what recent version of `.Net` (4.5+) you can look into changing the signature of your method `Add` to be `public async Task Add(int number1, int number2)` then in your form when the button is pressed, you can use async/await to await the result of your Add method which will free up the UI thread so that the application remains responsive and update the ui from inside the form event when the value is returned from the async method. – Ryan Wilson Sep 10 '18 at 20:04
  • If more than one way to do something persists, that means each way has its own advantages and disadvantages, and there is no one “best” way. The example `return number1 + number2` is not useful for making decisions as to what is the best way to handle “long running processes” we have no information about. – Dour High Arch Sep 10 '18 at 20:23
  • I've added a couple of duplicate links that I think you'll find helpful, but the short answer to your question is: if you want this to scale, get away from stuffing application logic into the forms; else you'll quickly end up with a web of inter-dependencies that'll sap your energy and cause you to yearn for a career in alpine goat farming. Lots of different ways to go about that; the linked answers should give you a start on various patterns to investigate - pick the ones that best meet your needs / preferences. – Shog9 Sep 10 '18 at 20:56

1 Answers1

-1
label1.Text = calculatorinstance.Add(x, y).ToString();

or

label1.Text = $"{calculatorinstance.Add(x, y)}";

Your talk of Threads, Delegates, Invoke is only relevant if you plan on using background or long running processes. You don't mention that you are doing these long running processes so the above should suffice.

Handbag Crab
  • 1,488
  • 2
  • 8
  • 12
  • Thanks for the answer! Actually right now I'm working on quite a big WinForms project (a chess program). Should I use the long running processes stuff or am I good to go if I use your example? – parhine Sep 10 '18 at 20:10
  • If you're doing the Add on a background thread then you can't update your label from the background thread as your application will crash with a cross thread exception. You must Invoke or BeginInvoke the label1.Text call `this.BeginInvoke(new Action() => label1.Text = calculatorinstance.Add(x, y).ToString());` or something very similar to that. With a chess program I'd assume that it's not got a lot to do so no need for background threads. I.E. user makes move, computer calculates move makes move. I doubt the computer part would be taxing too much. – Handbag Crab Sep 10 '18 at 20:17