Search code examples
c#byref

Pass by Ref Textbox.Text


I currently have something that I want to pass a textbox.text by ref. I don't want to pass the whole textbox and I want the function to change the text along with returning the other variable.

    public int function(int a, int b, string text)
    {
        //do something

        if (a + b > 50)
        {
            text = "Omg its bigger than 50!";
        }

        return (a + b);
    }

Is there a way to pass the Textbox.text by ref and change it inside the function?


Solution

  • You can't pass a property by ref, only a field or a variable.

    From MSDN :

    Properties are not variables. They are actually methods, and therefore cannot be passed as ref parameters.

    You have to use an intermediate variable :

    string tmp = textBox.Text;
    int x = function(1, 2, ref tmp);
    textBox.Text = tmp;