Search code examples
c#variablesparameter-passingref-parameters

Cant seem to pass parameters without errors


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

Solution

  • 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;
    }