Highlighted
Respected Contributor.
Respected Contributor.
509 views

String to binary-doubled

Jump to solution

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.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: String to binary-doubled

Jump to solution

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.

View solution in original post

7 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: String to binary-doubled

Jump to solution

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

 

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: String to binary-doubled

Jump to solution
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.
0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

Re: String to binary-doubled

Jump to solution

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

 

0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

Re: String to binary-doubled

Jump to solution

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

 

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: String to binary-doubled

Jump to solution

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.

View solution in original post

Highlighted
Respected Contributor.
Respected Contributor.

Re: String to binary-doubled

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

and it worked! Thank you!
0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

Re: String to binary-doubled

Jump to solution

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

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.