How do I convert a int to a string?

C

COHENMARVIN

I tried the following:
int myInt;
MessageBox.Show(myInt);
But this does not work, because a string is expected. How do I
convert myInt to a string? I tried myInt.toString and that is not
accepted, and neither is (string) myInt.

Another thing I tried was:
String MyString;
MyString = "Hello"
MessageBox.Show(MyString[3]);
And this also does not work, because a char cannot be converted to a
string.

But there must be a way to display these.
Thanks,
Marvin
 
Z

zeero

Hi,
did you get any error like "use of unassigned variable" ? Look in the error
window.
If you write:
int myint = 0;
MessageBox.Show(myint.ToString());

This should work.

...ab
 
M

Martin Honnen

I tried the following:
int myInt;
MessageBox.Show(myInt);
But this does not work, because a string is expected. How do I
convert myInt to a string? I tried myInt.toString

myInt.ToString()
 
M

Michel Walsh

Every object has ToString(). For an integer, that does the intended job:

MessageBox.Show(myInt.ToString());


Vanderghast, Access MVP
 
P

Peter Duniho

I tried the following:
int myInt;
MessageBox.Show(myInt);
But this does not work, because a string is expected. How do I
convert myInt to a string? I tried myInt.toString and that is not
accepted, and neither is (string) myInt.

"myInt.ToString()" should work fine. If you're having a problem using it,
you should post a code sample showing exactly how you're trying to use it,
along with details regarding the specific error you're getting.

You can use the Convert class if you want, but IMHO it's overkill.

Pete
 
R

Rad [Visual C# MVP]

I tried the following:
int myInt;
MessageBox.Show(myInt);
But this does not work, because a string is expected. How do I
convert myInt to a string? I tried myInt.toString and that is not
accepted, and neither is (string) myInt.

Another thing I tried was:
String MyString;
MyString = "Hello"
MessageBox.Show(MyString[3]);
And this also does not work, because a char cannot be converted to a
string.

But there must be a way to display these.
Thanks,
Marvin

An even better way is to use the String.Format method like so:

MessageBox.Show(string.Format("{0:#,#.00}"));

Reason being you can control things like thousand separators and so forth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top