471,325 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

imbed variables in text inside of MessageBox(

Just a quick question, im just taking a C# class.

Is there a way to have a variable inside a text string like:
Console.Write("Answer: {0}",answer);

But instead do it inside of a MessageBox.Show(

Thanks
Jan 24 '06 #1
7 16737
generallee5686,

You can always format the string before you make the call to Show. So,
you could do this:

string prompt = string.Format("Answer: {0}", answer);
MessageBox.Show(prompt);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"generallee5686" <u18038@uwe> wrote in message news:5ada7c985ce28@uwe...
Just a quick question, im just taking a C# class.

Is there a way to have a variable inside a text string like:
Console.Write("Answer: {0}",answer);

But instead do it inside of a MessageBox.Show(

Thanks

Jan 24 '06 #2
generallee5686,

MessageBox doesn't provide this out of the box, but it can be easily
workaround using String.Format method

Message.Box.Show(string.Format("Answer: {0}",answer"));
--
HTH
Stoitcho Goutsev (100)

"generallee5686" <u18038@uwe> wrote in message news:5ada7c985ce28@uwe...
Just a quick question, im just taking a C# class.

Is there a way to have a variable inside a text string like:
Console.Write("Answer: {0}",answer);

But instead do it inside of a MessageBox.Show(

Thanks

Jan 24 '06 #3
generallee5686 <u18038@uwe> wrote:
Just a quick question, im just taking a C# class.

Is there a way to have a variable inside a text string like:
Console.Write("Answer: {0}",answer);

But instead do it inside of a MessageBox.Show(


Use string.Format:

MessageBox.Show (string.Format ("Answer: {0}", answer),
...);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 24 '06 #4
Wow that helped tons guys.
Thanks so much!
Jan 24 '06 #5
I like to build a function that takes params....

private void ShowMsg(string fmt, params object[] prms)
{
MessageBox.Show(string.Format(fmt, prms));
}

ShowMsg("Answer: {0}", answer);

-mdb

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
generallee5686 <u18038@uwe> wrote:
Just a quick question, im just taking a C# class.

Is there a way to have a variable inside a text string like:
Console.Write("Answer: {0}",answer);

But instead do it inside of a MessageBox.Show(


Use string.Format:

MessageBox.Show (string.Format ("Answer: {0}", answer),
...);


Jan 24 '06 #6
Just another quick question about that.

So does like Console.Write have string.Format built into it or something?
Jan 25 '06 #7
generallee5686 <u18038@uwe> wrote:
Just another quick question about that.

So does like Console.Write have string.Format built into it or something?


Basically, yes. The overload
Console.Write (string, params object[])
calls through to TextWriter.Write(string, params object[])
which in turn calls string.Format.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by =?Utf-8?B?YWxiZXJ0b3Nvcmlh?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.