Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 似乎无法无错误地传递参数_C#_Variables_Parameter Passing_Ref Parameters - Fatal编程技术网

C# 似乎无法无错误地传递参数

C# 似乎无法无错误地传递参数,c#,variables,parameter-passing,ref-parameters,C#,Variables,Parameter Passing,Ref Parameters,我试图编写一个简单的程序,不熟悉通过方法传递参数。这是我目前为止在button click方法下得到的结果,但它返回错误,例如:使用未分配的局部变量(对于strColor、strMake和decPrice)以及“类型或命名空间定义,或预期的文件结尾”,但我所有的括号都是正确的。谢谢你的帮助 private void btnSubmit_Click(object sender, EventArgs e) { string strColor; string

我试图编写一个简单的程序,不熟悉通过方法传递参数。这是我目前为止在button click方法下得到的结果,但它返回错误,例如:使用未分配的局部变量(对于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");
        }
    }

您正在方法中声明方法。
DisplayResult
GetPrice
GetMake
GetColor
的声明从
btnSubmit\u的声明中移出,然后单击

在方法中声明方法。
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");
}

DisplayResult
GetPrice
GetMake
GetColor
的声明从
btnSubmit\u的声明中移出,然后单击

在方法中声明方法。
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");
}

DisplayResult
GetPrice
GetMake
GetColor
的声明从
btnSubmit\u的声明中移出,然后单击

在方法中声明方法。
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");
}

DisplayResult
GetPrice
GetMake
GetColor
的声明移出
btnSubmit\u单击

您不能将函数放在其他函数中。这就是您正在做的-引用代码的第一行,“private void btnSubmit_Click(object sender,EventArgs e)”,正在定义一个函数,您正在尝试将其他函数放入其中。您想要在“DisplayResult(strColor、strMake、decPrice)”之后的结尾};

您不能将函数放在其他函数中。这就是您正在做的事情-引用代码的第一行,“private void btnSubmit\u Click(object sender,EventArgs e),正在定义一个函数,而您正试图将其他函数放入其中。您想要在“DisplayResult(strColor、strMake、decPrice)”之后的结尾};

您不能将函数放在其他函数中。这就是您正在做的事情-引用代码的第一行,“private void btnSubmit\u Click(object sender,EventArgs e),正在定义一个函数,而您正试图将其他函数放入其中。您想要在“DisplayResult(strColor、strMake、decPrice)”之后的结尾};

您不能将函数放在其他函数中。这就是您正在做的事情-引用代码的第一行,“private void btnSubmit\u Click(object sender,EventArgs e),正在定义一个函数,而您正试图将其他函数放入其中。您希望在“DisplayResult(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");
            }
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");
}

不能在方法中包含方法,也不能将其移动到按钮调用之外

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

不能在方法中包含方法,也不能将其移动到按钮调用之外

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

不能在方法中包含方法,也不能将其移动到按钮调用之外

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");
            }
使用时,需要初始化传递给被调用函数的参数

所以你需要

string strColor = string.Empty;
string strMake = string.Empty;
decimal decPrice = 0;
当然,您不能在另一个函数中包含函数。您应该提取按钮事件处理程序中的方法,并将它们放在
btnSubmit\u 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;
}
使用时,需要初始化传递给被调用函数的参数

所以你需要

string strColor = string.Empty;
string strMake = string.Empty;
decimal decPrice = 0;
当然,您不能在另一个函数中包含函数。您应该提取按钮事件处理程序中的方法,并将它们放在
btnSubmit\u 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;
}
使用时,需要初始化传递给被调用函数的参数

所以你需要

string strColor = string.Empty;
string strMake = string.Empty;
decimal decPrice = 0;
当然,您不能在另一个函数中包含函数。您应该提取按钮事件处理程序中的方法,并将它们放在
btnSubmit\u 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;
}
使用时,需要初始化传递给被调用函数的参数

所以你需要

string strColor = string.Empty;
string strMake = string.Empty;
decimal decPrice = 0;
当然,您不能在另一个函数中包含函数。您应该提取按钮事件处理程序中的方法,并将它们放在
btnSubmit\u 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;
}

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

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

这段代码有很多错误。首先,正如其他人所说,您试图在metho中嵌套方法