all groups > c# > january 2006 >
You're in the

c#

group:

imbed variables in text inside of MessageBox(


Re: imbed variables in text inside of MessageBox( Michael Bray
1/24/2006 2:28:39 PM
c#:
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] <skeet@pobox.com> wrote in
news:MPG.1e40b0fe818607ee98cd61@msnews.microsoft.com:

[quoted text, click to view]
Re: imbed variables in text inside of MessageBox( Nicholas Paldino [.NET/C# MVP]
1/24/2006 4:41:07 PM
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]
- mvp@spam.guard.caspershouse.com

[quoted text, click to view]

Re: imbed variables in text inside of MessageBox( Stoitcho Goutsev (100)
1/24/2006 4:46:32 PM
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)

[quoted text, click to view]

imbed variables in text inside of MessageBox( generallee5686
1/24/2006 9:31:47 PM
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(

Re: imbed variables in text inside of MessageBox( Jon Skeet [C# MVP]
1/24/2006 9:48:19 PM
[quoted text, click to view]

Use string.Format:

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: imbed variables in text inside of MessageBox( generallee5686
1/24/2006 9:53:49 PM
Wow that helped tons guys.
Re: imbed variables in text inside of MessageBox( Jon Skeet [C# MVP]
1/25/2006 12:00:00 AM
[quoted text, click to view]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: imbed variables in text inside of MessageBox( generallee5686
1/25/2006 2:23:38 AM
Just another quick question about that.

AddThis Social Bookmark Button