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] > 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),
> ...);
>
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] "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
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] "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
[quoted text, click to view] 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 - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet