I have a function, that reads the Text from a TextBox and then tries to parse it to a number. If that is possible, it will store the parsed data. If it is not possible, the data is not touched, instead the TextBox.Text will be set to the String-Representation of the variable. As I need it for various TextBoxes and underlying Variables my function looks like this:
public bool ValidateAndParseTextBox(TextBox textBox, ref double storage)
{
try
{
storage = double.Parse(textBox.Text);
return true;
}
catch (FormatException)
{
textBox.Text = storage.ToString();
return false;
}
}
And the same for int
Now I moved the underlying data to a seperate object, and therefore I wanted to use properties instead of just having the variables public. With those however I can't use the ref
statement.
If I didn't need the boolean return
value, I could just do it like this
public double ValidateAndParseTextBox(TextBox textBox, double Value)
{
double ret;
if (!double.TryParse(textBox.Text, ret))
return Value;
return ret;
}
But I somehow fail to see a solution with the bool value. Any Ideas? What would be nice, if I somehow could send the property setter as delegate to a function, or something like that.
thanks, -m-