0

我正在尝试编写一个简单的程序,并且不熟悉通过方法传递参数。到目前为止,这是我在按钮单击方法下的内容,但它返回错误,例如:使用未分配的局部变量(用于 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");
        }
    }
4

5 回答 5

2

When using the ref keyword you need to initialize the parameter passed to the called function.

So you need

string strColor = string.Empty;
string strMake = string.Empty;
decimal decPrice = 0;

Of course you cannot have a function inside another function. You should extract the methods inside the event handler of the button and put them at the same level of the 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");
}

However, your use of the ref keyword seems to be pointless. Just use the return statement and change the methods to return the appropriate values and assign then to the correct variables

... 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;
}
于 2013-11-08T20:58:35.557 回答
1

You can't put functions inside other functions. Which is what you're doing - the first line of your quoted code, "private void btnSubmit_Click(object sender, EventArgs e)", is defining a function, and you're trying to put other functions inside it. You want the end } after "DisplayResult(strColor, strMake, decPrice);

于 2013-11-08T20:56:44.423 回答
0

you cannot have a method inside a methods move it outside the button call

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");
            }
于 2013-11-08T20:57:01.180 回答
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"));
}
于 2013-11-08T21:06:06.217 回答
0

您在方法中声明方法。将DisplayResult, GetPrice,GetMakeGetColor的声明移出btnSubmit_Click

于 2013-11-08T20:56:11.130 回答