xsharp.eu • How to allow for functions to return String or Binary results
Page 1 of 1

How to allow for functions to return String or Binary results

Posted: Sun Nov 01, 2020 5:09 pm
by atlopes
I'm trying to implement the VFP's STRCONV() function.

Depending on the arguments passed to the function, I want the result to be either of String or Binary type.

I declared the functions as

Code: Select all

FUNCTION StrConv (Expression AS String, ConversionSetting AS Int, RegionalIdentifier AS Int, RegionalIDType AS Int) AS USUAL
...
FUNCTION StrConv (Expression AS Binary, ConversionSetting AS Int, RegionalIdentifier AS Int, RegionalIDType AS Int) AS USUAL
When the result is a String, as in the Single Byte to Base64 conversion, I can do whatever I need with the result and use it anywhere an expression is expected.

When the result is a Binary, as in Base64 to Single Byte conversion, there is not much I can do with the result except displaying it. If i try to use what's returned in some other context, an assignment into a Binary variable, for instance, a runtime error System.InvalidCastException pops up.

Code: Select all

? StrConv("Abcd", STRCNV_SB_BASE64)   && displays QWJjZA==, which is good
? StrConv("QWJjZA==", STRCNV_BASE64_SB)   && displays 0h41626364, which is also good
* but it seems that there is nothing else besides displaying that this returned value can do
Is there is anything I'm missing in the interpretation of what the USUAL type is able to do? Or is it something still missing from the implementation of the Binary type?

How to allow for functions to return String or Binary results

Posted: Sun Nov 01, 2020 8:10 pm
by robert
Antonio,
I think I have forgotten to implement the implicit converter from Usual to Binary.
Sorry about that.
You can add this binary inside the usual to another binary and/or to a string. That works.
But to retrieve the value as binary is not possible without "special tricks" at this moment.
The only thing that you can do now is to add an extra cast:

LOCAL binValue as Binary
binValue := Binary{ (byte[] ) (OBJECT) usualValue}

Of course I will add the implicit converter in the next build.

Robert

How to allow for functions to return String or Binary results

Posted: Mon Nov 02, 2020 8:30 am
by atlopes
Robert, thank you.

I'll try to use the workaround in current development & testing.