Cant seem to pass parameters without errors

108 Views Asked by At

I am trying to write a simple program and am unfamiliar with passing parameters through methods. This is what I have so far under the button click method but it returns errors such as: Use of unassigned local variable (for strColor, strMake, and decPrice) as well as "Type or namespace definition, or end-of-file expected" but I have all of my brackets correct. Thanks for your help!

 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

There are 5 best solutions below

1
On BEST ANSWER

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;
}
0
On

You are declaring methods within methods. Move the declaration of DisplayResult, GetPrice, GetMake, and GetColor out of the declaration for btnSubmit_Click

0
On

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

1
On

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

There are a number of things wrong with this code. Firstly, as others have said, you have tried to nest methods inside methods. The other problem is that you are using ref to pass values out of methods, rather than using a return type.

The methods to initialise strColor and strMake aren't really needed and can be "in-lined" and GetPrice can be improved by adding a return type to it.

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