I have a little simple method where I would like to pass in a label.
Now by my reading, anything based off the object class should automatically pass by reference, so this was my first try at my method:
public static void ValiateStepAsInt(String Step, int? Value, Label Error)
{
if (Value == null && Step != "")
{
Error.Text = "Error!!!";
return;
}
Error.Text = "";
}
I didn’t get any compilor errors, but whenever i called this, it would set the Error.text, but once it got out of the method, that value would disappear (so not really pass by reference).
So next I added the “ref” option to the Label Error; hoping this might fix the issue, but still the same thing, the method sets it, but once its done, ths value disappears.
What I am missing when trying to pass a label by reference?
You’re resetting the
Textproperty to an empty string just below your conditional statement. Maybe you want to wrapError.Text = "";into anelse.If that’s not it, then perhaps you’re setting the value somewhere else as well and thus overwrite the change in the method you’ve shown us here.