【问题标题】:Cant seem to pass parameters without errors [closed]似乎无法毫无错误地传递参数[关闭]
【发布时间】: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


【解决方案1】:

使用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;
}

【讨论】:

  • 谢谢,现在修复了通常的复制/粘贴错误
【解决方案2】:

您不能将函数放入其他函数中。这就是您正在做的事情-您引用的代码的第一行“private void btnSubmit_Click(object sender, EventArgs e)”正在定义一个函数,并且您试图将其他函数放入其中。您希望在 "DisplayResult(strColor, strMake, decPrice); 之后结束 };

【讨论】:

    【解决方案3】:

    您在方法中声明方法。 将DisplayResultGetPriceGetMakeGetColor 的声明移出btnSubmit_Click 的声明

    【讨论】:

      【解决方案4】:

      您不能在方法中使用方法将其移到按钮调用之外

      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");
                  }
      

      【讨论】:

      • 我这样做了,但是使用未分配变量的错误仍然存​​在。
      【解决方案5】:

      这段代码有很多问题。首先,正如其他人所说,您尝试将方法嵌套在方法中。另一个问题是您使用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"));
      }
      

      【讨论】:

        猜你喜欢
        • 2019-11-25
        • 1970-01-01
        • 2021-03-20
        • 2013-10-28
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多