String to binary-doubled

I have declared in the working storage of my Windows form a numeric variable as follows:

01 tmp-Num pic 9(10)

 

I am trying to move the text from a text control to a numeric field variable a follows:

 

  Set tmp-Num  to txt_CLNT-NAME-SELECT::Text.

 

But I received a “cannot implicitly convert string to binary-doubled unsigned”. I type a 10 numeric digit in the txt_CLNT-NAME-SELECT but my form, it seems, is defining it as alphabetic characters. I only want numeric fields to be typed into the  txt_CLNT-NAME-SELECT::Text because I need to do calculation with it.

  • You could do:

    Set tmp-Num to binary-double unsigned::Parse(txt_CLNT-NAME-SELECT::Text)

    but unless there's a way to enforce a non-negative numeric value on the form field, that could throw an exception. You can catch the exception, but I'd use:

    if not binary-double unsigned::TryParse(txt_CLNT-NAME-SELECT::Text, output tmp-Num)
        error handling
    end-if

     

  • I'm assuming COBOL for .NET here. If this is COBOL for JVM, you'd have to use whatever integer-parsing methods the JRE provides.
  • Michael,

    Based on my experiences with this type of situation, that "output" field in your example needs to be a managed field, such as:

    01     bdu                binary-double unsigned.

    and then, if no error, move the value in bdu to tmp-Num.

    I'm having a problem getting your example to compile as specified.

    What am I missing?

    Thanks,

    Austin

     

  • Correction to my reply above.

    Remove the word "output" and your example will compile and run.

    One side effect is that it will zero out the receiving field if invalid.

    Thanks,

    Austin

     

  • Verified Answer

    Huh. I don't know why it works with "output" removed. It's an output parameter; the output qualifier should be required. Sounds like the COBOL compiler is doing something clever with a temporary behind the scenes.

    Anyway, I would have done something like this:

    declare tmp-BDU as binary-double unsigned
    if binary-double unsigned::TryParse(txt_CLNT-NAME-SELECT::Text, output tmp-BDU)
        set tmp-Num to tmp-BDU
    else
        error handling
    end-if

    (Basically what you did, but I prefer using the declare statement over traditional COBOL declarations for managed types.)

    A more traditional-COBOL way to do it would be to use FUNCTION NUMVAL:

    move FUNCTION NUMVAL(txt_CLNT-NAME-SELECT::Text) to tmp-Num

    which should be equivalent for most purposes. Though I don't remember offhand what happens with intrinsic functions like NUMVAL when the argument is out of range (e.g. not numeric in this case) - a runtime error? You might have to test the value with IS NUMERIC first, so it's not like this is any more concise than the .NET way.

  • I used your example:
    Set tmp-Num to binary-double unsigned::Parse(txt_CLNT-NAME-SELECT::Text

    and it worked! Thank you!
  • Michael,

    Just to clarify, that "if tryparse" statement:

    ... will compile using tmp-Num and without the "output" specification

    ...  but will NOT compile using tmp-Num with the "output" specification

    ... will compile using your tmp-BDU either with or without the "output" specification.

    Strange, I agree.  But I think it has something to do with the type of the receiving field and therefore whether .Net or COBOL will be in charge of doing the conversion.

    In C#, a solution might be something like:

    ulong bdu;
    if (!ulong.TryParse(textBox1.Text, out bdu))
        MessageBox.Show("Error!");
    else
        MessageBox.Show("OK");
     

    Thanks,

    Austin