How to allow for functions to return String or Binary results

This forum is meant for questions about the Visual FoxPro Language support in X#.

Post Reply
atlopes
Posts: 83
Joined: Sat Sep 07, 2019 11:43 am
Location: Portugal

How to allow for functions to return String or Binary results

Post 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?
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

How to allow for functions to return String or Binary results

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
atlopes
Posts: 83
Joined: Sat Sep 07, 2019 11:43 am
Location: Portugal

How to allow for functions to return String or Binary results

Post by atlopes »

Robert, thank you.

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