【发布时间】:2013-11-21 00:12:59
【问题描述】:
我正在尝试编写一个简单的程序,但不熟悉通过方法传递参数。到目前为止,这是我在按钮单击方法下的内容,但它返回错误,例如:使用未分配的局部变量(用于 strColor、strMake 和 decPrice)以及“类型或命名空间定义,或预期的文件结尾”但我所有的括号都是正确的。感谢您的帮助!
private void btnSubmit_Click(object sender, EventArgs e)
{
string strColor;
string strMake;
decimal decPrice;
GetColor(ref strColor);
GetMake(ref strMake);
GetPrice(ref decPrice);
DisplayResult(strColor, strMake, decPrice);
private void GetColor(ref string color){
color = lstColor.SelectedItem.ToString();
}
private void GetMake(ref string make){
make = lstMake.SelectedItem.ToString();
}
private void GetPrice(ref decimal price){
if (decimal.TryParse(txtMaxPrice.Text, out price)){
}
else{
MessageBox.Show("Enter a valid number");
}
}
private void DisplayResult(string color, string make, decimal price){
lblMessage.Text = "Color of " + color + " Make of: " + make + " " + price.ToString("c");
}
}
【问题讨论】:
-
你在方法中声明了方法还是错字?
-
我就是这么做的。所以这更像是一个不可能的学习错字:)
标签: c# variables parameter-passing ref-parameters