我正在尝试编写一个简单的程序,并且不熟悉通过方法传递参数。这是我到目前为止在按钮单击方法下但它返回的错误,例如:使用未分配的局部变量(对于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");
}
}
答案 0 :(得分:2)
使用ref keyword时,需要初始化传递给被调用函数的参数。
所以你需要
string strColor = string.Empty;
string strMake = string.Empty;
decimal decPrice = 0;
当然你不能在另一个函数中有一个函数。您应该在按钮的事件处理程序中提取方法,并将它们放在btnSubmit_Click
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");
}
但是,使用ref关键字似乎毫无意义。只需使用return语句并更改方法以返回适当的值,然后将其分配给正确的变量
... in btnSubmit_Click
string strColor = GetColor();
string strMake = GetMake();
decimal decPrice = GetPrice();
if(decPrice != 0)
.....
private string GetColor()
{
return lstColor.SelectedItem.ToString();
}
private string GetMake()
{
return lstMake.SelectedItem.ToString();
}
private decimal GetPrice()
{
decimal price;
if(!decimal.TryParse(txtMaxPrice.Text, out price))
{
MessageBox.Show("Enter a valid number");
}
return price;
}
答案 1 :(得分:1)
您不能将函数放在其他函数中。你正在做的是 - 你引用的代码的第一行,“private void btnSubmit_Click(object sender,EventArgs e)”,正在定义一个函数,你正在尝试将其他函数放入其中。你希望在“DisplayResult(strColor,strMake,decPrice)之后结束};
答案 2 :(得分:0)
您正在声明方法中的方法。
将DisplayResult
,GetPrice
,GetMake
和GetColor
的声明移出btnSubmit_Click
的声明
答案 3 :(得分:0)
你不能在方法内部使用方法将其移出按钮调用
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");
}
答案 4 :(得分:0)
此代码存在许多问题。首先,正如其他人所说,你试图在方法中嵌套方法。另一个问题是您使用ref
从方法中传递值,而不是使用返回类型。
初始化strColor和strMake的方法并不是真正需要的,可以“内联”,并且可以通过向其添加返回类型来改进GetPrice
。
private void btnSubmit_Click(object sender, EventArgs e)
{
string strColor = lstColor.SelectedItem.ToString();
string strMake = lstMake.SelectedItem.ToString();
decimal decPrice = GetPrice(();
DisplayResult(strColor, strMake, decPrice);
}
private decimal GetPrice()
{
decimal price;
if (!decimal.TryParse(txtMaxPrice.Text, out price))
{
MessageBox.Show("Enter a valid number");
}
return price;
}
private void DisplayResult(string color, string make, decimal price)
{
lblMessage.Text = string.Format("Color of {0} Make of: {1} {2}",
color, make, price.ToString("c"));
}