如何将值作为文本而不是void?
例:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = myvoid("foo", true);
//Error, Cannot imlicity convert type void to string
}
public void myvoid(string key , bool data)
{
if (data == true)
{
string a = key + " = true";
MessageBox.Show(a); //How to export this value to labe1.Text?
}
else
{
string a = key + " = false";
MessageBox.Show(a); //How to export this value to labe1.Text?
}
}
Run Code Online (Sandbox Code Playgroud)
如何从返回void的方法中指定值a,而不是显示消息框,并将其应用于label1.Text?
public string myvoid(string key, bool data)
{
return key + " = " + data;
}
Run Code Online (Sandbox Code Playgroud)
此外,您的方法不应再被调用myvoid,因为它实际上返回一个值.有点像FormatValue会更好.
private void button1_Click(object sender, EventArgs e)
{
label1.Text = myvoid("foo", true);
}
public string myvoid(string key , bool data)
{
if (data)
return key + " = true";
else
return key + " = false";
}
Run Code Online (Sandbox Code Playgroud)
正如奥斯丁在评论中提到的那样,这将更加清洁
public string myvoid(string key , bool data)
{
return string.Format("{0} = {1}", key, data);
}
Run Code Online (Sandbox Code Playgroud)