0

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");
}
8
  • I assume SPos TPos and Increment are properties of the same class? Commented May 16, 2018 at 11:59
  • BTW "txtSPos.Text" should be txtSPos.Text Commented May 16, 2018 at 12:01
  • What problem you have with the double parameter. I'm afraid it's not clear at all. I also don't understand: "I must key in an actual number when I recall the method" Commented May 16, 2018 at 12:04
  • I think he is using an autoproperty, which is why he can't pass it as out Commented May 16, 2018 at 12:06
  • Hi Sebastian, if you see the working code, SPos, TPos and Increment are just names I gave for the "out doubles". I can name them anything I like in the "out double" as long as I recall the same names in the functions. Hi Rahul, thanks for the tip but I still can't figure out a method to make all this work. Commented May 16, 2018 at 12:07

1 Answer 1

0

Though not entirely clear but I think you want to replace the out variable name in your TryParse() method per the argument supplied in method. In that case, you can use a switch statement like below. Though, don't see any reason for doing that

private void TryParseDouble(string ParseTarget, string PointField, string FieldInMsg)
{
 bool resBool; 
  switch (PointField)
  {
      case "SPos":
          resBool = double.TryParse(ParseTarget, out double SPos);
          break;
      case "TPos":
          resBool = double.TryParse(ParseTarget, out double TPos);
          break;
      case "Increment":
          resBool = double.TryParse(ParseTarget, out double Increment);
          break;
      default:              
          break;
  }

    if (!resBool) FalseBoolMsg(FieldInMsg);
}

You should call it like

TryParseDouble(txtTPos.Text, "TPos", "Target Position");
Sign up to request clarification or add additional context in comments.

5 Comments

Why not passing the double as out parameter into this method? Then you also get the value which was parsed. I don't understand this question though.
@TimSchmelter it's more of an guessed answer and so said in post clearly.
What is the benefit of this method? It just allows to make mistakes and doesn't give anything useful back. Sure, the FalseBoolMsg call, but that could be achieved easier with a out double parameter.
Hi Rahul, thanks for the suggestion. I think it will work but other than "SPos", "TPos" and "Increment" I have many other "double" variables I need to name besides those three, meaning I'll have to add many cases.
@Stoverflow, in that case would suggest you to rethink your design once cause what you are saying doesn't looks correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.