-2

How could I fix the error upon compiling the short code.

Here is the code:

 private void button1_Click(object sender, EventArgs e)
 {
      int a = 5;
      MessageBox.Show(a.ToString);
 }

It gives me back this 2 errors:

Error 1 The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string)' has some invalid argumentsError 2 Argument 1: cannot convert from 'method group' to 'string'

Any idea?

John Woo
  • 258,903
  • 69
  • 498
  • 492
Keydi
  • 21
  • 3
  • 6

2 Answers2

3

You are missing () at the end of ToString

MessageBox.Show(a.ToString());

The error you are getting:

Argument 1: cannot convert from 'method group' to 'string'

That is because MessageBox.Show expects a string type parameter and since you forgot to specify () at the end, a.ToString is a method group.

Got the following for Method group by Marc Gravell from a question on Stackoverflow.

A method group is the name for a set of methods (that might be just one) - i.e. in theory the ToString method may have multiple overloads (plus any extension methods): ToString(), ToString(string format), etc - hence ToString by itself is a "method group".

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
2

you lack () in the ToString() method,

private void button1_Click(object sender, EventArgs e)
 {
      int a = 5;
      MessageBox.Show(a.ToString()); // <<== HERE
 }
John Woo
  • 258,903
  • 69
  • 498
  • 492