Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#消息框,变量用法_C#_Variables_Messagebox - Fatal编程技术网

C#消息框,变量用法

C#消息框,变量用法,c#,variables,messagebox,C#,Variables,Messagebox,我已经搜索过了,但我不知道我是否使用了正确的措辞来搜索。我正在为我的班级编写一个C#程序,但我在使用消息框时遇到了问题 我试图让消息框显示一条消息,同时读取一个变量。在控制台应用程序中这样做没有问题,但在Windows端我无法解决 到目前为止,我已经: MessageBox.Show("You are right, it only took you {0} guesses!!!", "Results", MessageBoxButtons.OK); 这很好用。然而,我试图让{0}成为变量num

我已经搜索过了,但我不知道我是否使用了正确的措辞来搜索。我正在为我的班级编写一个C#程序,但我在使用消息框时遇到了问题

我试图让消息框显示一条消息,同时读取一个变量。在控制台应用程序中这样做没有问题,但在Windows端我无法解决

到目前为止,我已经:

MessageBox.Show("You are right, it only took you {0} guesses!!!", "Results", MessageBoxButtons.OK);
这很好用。然而,我试图让{0}成为变量numguesss的结果。我确信这很简单,我只是在书中或其他地方忽略了它,或者我在某个地方的语法不正确。

试试看

MessageBox.Show(string.Format ("You are right, it only took you {0} guesses!!!", numGuesses ), "Results", MessageBoxButtons.OK);
请参见

关于什么

这就是你想要的:

string message = string.Format("You are right, it only took you {0} guesses!!!",numGuesses)

MessageBox.Show(message, "Results", MessageBoxButtons.OK);

您可以使用
String.Format
或简单的字符串连接

MessageBox.Show(String.Format("You are right, it only took you {0} guesses!!!", myVariable), "Results", MessageBoxButtons.OK);

串联:

MessageBox.Show("You are right, it only took you " + myVariable + " guesses!!!", "Results", MessageBoxButtons.OK);

这两个结果是等效的,但是如果在同一个字符串中有多个变量,您可能更喜欢
String.Format

您的类已经很好地完成了,并且这个线程已经很旧了。话虽如此,今天您可以这样解决这个问题:

var firstName = "Sam";
MessageBox.Show($"My first name is { firstName }.", "First Name", MessageBoxButtons.Ok);
也可以在捕捉块中使用它:

...
}
catch (Exception ex)
{
    MessageBox.Show($"Program encountered an error \n\n{ ex.Message }", "Program Error", MessageBoxButtons.Ok);
}

基本上,任何字符串变量都可以这样使用。不确定返回字符串的函数,尽管我不明白为什么不可以。

MessageBox.Show(string.Format(“你是对的,它只需要你{0}次猜测!!!”,numguesss),“Results”,MessageBoxButtons.OK)我不会说它们是等价的。format调用使用stringbuilder,而concat创建字符串(可能较慢)。@Anthony Sottile:正确。我应该说结果是相等的。
var firstName = "Sam";
MessageBox.Show($"My first name is { firstName }.", "First Name", MessageBoxButtons.Ok);
...
}
catch (Exception ex)
{
    MessageBox.Show($"Program encountered an error \n\n{ ex.Message }", "Program Error", MessageBoxButtons.Ok);
}