Michael Garrison
Michael Garrison

Reputation: 941

C# No overload method 'ToString' takes 1 arguments - simple

I'm fairly new to C# and I came across an error that I don't know how to fix. As the title states, I get the No overload method 'ToString' takes 1 arguments error. I have looked at other questions on here but they are more complex then what I am trying to do. I have a simple equation that I am trying to show in a message box and it looks as follows:

Y = C + I + E + G;

MessageBox.Show(ToString(Y));   

All of the variables in the formula are integers, stored as int, and are taken from text boxes. I have been learning C# from thenewboston's tutorials on YouTube and I haven't seen this issue there, then again there are over 200 videos on C# and I haven't gotten that far yet. Any suggestions on error debugging would be greatly appreciated.

Upvotes: 1

Views: 3475

Answers (5)

GETah
GETah

Reputation: 21419

The posts above pretty much covered the solution to your problem. On the error you are getting: When you call ToString(Y) without referring to a variable, the compiler will look at the current context which in your case is a class which inherits by default from Object see this link for details. So when you do ToString the compiler will look at Object.ToString() (if not overriden) which takes no parameter and that is why you get:

No overload method 'ToString' takes 1 arguments error

Upvotes: 2

NominSim
NominSim

Reputation: 8511

To provide a bit of an explanation: In C# everything has a ToString() method which you can call to give a string representation of the Object. Since you are calling ToString(Y), it complains that you are using the wrong number of arguments for the ToString method. In general, as others have pointed out, the way to represent an object as a String is to call Y.ToString().

It is good to note, that you can override the ToString() method within any class you make, so that calls to that classes ToString() method will return a more useful String.

Upvotes: 3

Ceramic Pot
Ceramic Pot

Reputation: 280

ToString is not an operator.It's a method of the object class.

Upvotes: 3

BluesRockAddict
BluesRockAddict

Reputation: 15683

Have you tried

MessageBox.Show(Y.ToString());  

Upvotes: 4

Steve
Steve

Reputation: 216293

The correct syntax is:

MessageBox.Show(Y.ToString());  

Upvotes: 13

Related Questions