-2

Hi I'm trying to multiply 2 numbers together in a simple winform application. The method for multiplying is in the class below and then called in the form when you click a button. I have label called answerText and I'm trying to print my answer in that. my line test.multiplynumbers.Tostring is wrong but not sure what to do there?

namespace WindowsFormsApp3
{
class Sums
{
    public int multiplynumbers(int num1, int num2)
    {
        return num1 * num2;
    }
}

}

and

namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void AnswerButton_Click(object sender, EventArgs e)
    {
        Sums test = new Sums();

        test.multiplynumbers(5, 2);
        answerText.Text = test.multiplynumbers.ToString;

    }
}
}

3 Answers3

0

Assign the result to a variable and then use ToString()

 int result = test.multiplynumbers(5, 2);
 answerText.Text = result.ToString();
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You can do:

int result = test.multiplynumbers(5, 2);

then

answerText.Text = result.ToString();

or

answerText.Text = Convert.ToString(result);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
-1

ToString is a method and therefore needs (). Update your code like this:

namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void AnswerButton_Click(object sender, EventArgs e)
    {
        Sums test = new Sums();

        int res = test.multiplynumbers(5, 2);
        answerText.Text = res.ToString();

    }
}
}
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55