These codes below work. But they are lengthy, so I wanted to create a method (lots of other TryParsing to do, this is just a small section).
private void button_Click(object sender, EventArgs e)
{
bool resSPos = double.TryParse(txtSPos.Text, out double SPos);
if (resSPos == false) FalseBoolMsg("Starting Position");
bool resTPos = double.TryParse(txtTPos.Text, out double TPos);
if (resTPos == false) FalseBoolMsg("Target Position");
bool resIncr = double.TryParse(txtIncr.Text, out double Increment);
if (resIncr == false) FalseBoolMsg("Increment");
Ch.FunctionA(Ch.FunctionX, SomeInt, Increment, Ch.FunctionY);
Ch.FunctionB(SomeInt, SPos, Ch.FunctionY);
Ch.FunctionA(0, SomeInt, TPos, Ch.FunctionZ);
}
"FalseBoolMsg" is just a method I defined higher-up for generating a MessageBox. "txtSPos", "txtTPos" and "txtIncr" are just TextBoxes in my Windows Form. Anyway, below is what I attempted but failed. I've tried several variations but to no avail. Mainly, I have a problem with the 'double' parameter more so than the string ones.
private void TryParseDouble(string ParseTarget, string PointField, string FieldInMsg)
{
bool resBool = double.TryParse(ParseTarget, out double PointField);
if (resBool == false) FalseBoolMsg(FieldInMsg);
}
private void button_Click(object sender, EventArgs e)
{
TryParseDouble("txtSPos.Text", "SPos", "Starting Position");
TryParseDouble("txtTPos.Text", "TPos", "Target Position");
TryParseDouble("txtIncr.Text", "Increment", "Increment");
Ch.FunctionA(Ch.FunctionX, SomeInt, Increment, Ch.FunctionY);
Ch.FunctionB(SomeInt, SPos, Ch.FunctionY);
Ch.FunctionA(0, SomeInt, TPos, Ch.FunctionZ);
}
Yes, I can change "string PointField" to "double PointField" in my method but that means I must key in an actual number when I recall the method, instead of typing in the name to replace "PointField". I also need the functions to read the "double Name" produced by the TryParse from my method. Thank you for your consideration.
Edit: I've found the answer thanks to John (What is the proper method or keyword to put in a user-defined method that renames a variable in C#?)
private bool TryParseDouble(string ParseTarget, out double PointField, string FieldInMsg)
{
if (!double.TryParse(ParseTarget, out PointField))
{
FalseBoolMsg(FieldInMsg);
return false;
}
return true;
}
private void button_Click(object sender, EventArgs e)
{
TryParseDouble(txtSPos.Text, out double SPos, "Starting Position");
}
SPosTPosandIncrementare properties of the same class?"txtSPos.Text"should betxtSPos.Text