Dynamic type casting for primitive data types

Posted by mdanwarh on 17-Aug-2018 14:15

Hello everyone,

In the below code snippet, is it possible to typecast the different type of fields in single statement? 

DEFINE VARIABLE hRecordBufferHandle AS HANDLE      NO-UNDO.

DEFINE VARIABLE hFieldBufferHandle  AS HANDLE      NO-UNDO.

DEFINE VARIABLE iFieldCounter AS INTEGER     NO-UNDO.

DEFINE VARIABLE vchFieldDataType AS CHARACTER NO-UNDO.

 

ASSIGN

    hRecordBufferHandle = BUFFER customer:HANDLE.

 

FOR EACH Customer WHERE custnum = 1 :

    DO iFieldCounter = 1 TO hRecordBufferHandle:NUM-FIELDS:

        hFieldBufferHandle = hRecordBufferHandle:BUFFER-FIELD(iFieldCounter).

      

        DO TRANSACTION:

                    vchFieldDataType =  hFieldBufferHandle:DATA-TYPE.

                    CASE vchFieldDataType :

                                WHEN "CHARACTER" THEN

                                DO:

                                                hFieldBufferHandle:BUFFER-VALUE =  STRING("SOME VALUE") NO-ERROR.

                                END.

                                WHEN "INTEGER" THEN

                                DO:

                                                hFieldBufferHandle:BUFFER-VALUE =  INTEGER("123") NO-ERROR.

                                END.

                                WHEN "INT64" THEN

                                DO:

                                                hFieldBufferHandle:BUFFER-VALUE =  INT64("123") NO-ERROR.

                                END.

                                WHEN "LOGICAL" THEN

                                DO:

                                                hFieldBufferHandle:BUFFER-VALUE =  LOGICAL("YES") NO-ERROR.

                                END.

                                WHEN "DATE" THEN

                                DO:

                                                hFieldBufferHandle:BUFFER-VALUE =  DATE("12/31/2018") NO-ERROR.

                                END.

                                WHEN "DATETIME-TZ" THEN

                                DO:

                                                hFieldBufferHandle:BUFFER-VALUE =  DATETIME-TZ(TODAY) NO-ERROR.

                                END.

                                WHEN "DECIMAL" THEN

                                DO:

                                                hFieldBufferHandle:BUFFER-VALUE =  DECIMAL("123") NO-ERROR.

                                END.

            END CASE.

           

        END.

    END.

END.


------------------------------------------------------------------------------------------------------------------------------------------

( i.e. Can i use something similar to this instead of assigning each field using case statement

hFieldBufferHandle:BUFFER-VALUE =  DYNAMIC-CAST(vchFieldValue ,  vchFieldDataType ) NO-ERROR. )

--------------------------------------------------------------------------------------------------------------------------------------------



Thanks in advance.

All Replies

Posted by Rick Terrell on 18-Aug-2018 08:40

[Shorthand to give you the point]


do i = 1 to num-fields:
   hBuff = buffer:buffet-field(i).
   case hBuff:data-type:
       when “integer” then
             Assign hBuff:buffer-value = integer(source).
       When “int64” then
      Etc 
      Etc 



Rick Terrell 
Principle Consultant, Professional Services 
Progress

Sent from my iPhone

Posted by mdanwarh on 18-Aug-2018 13:30

I am trying the same thing. But, I am wondering if we can do it in single statement. ( Dynamic-cast can be used to cast the object of similar types) . Is it possible for type casting the primitive data types dynamically ?

Posted by tpavlovic on 18-Aug-2018 14:20

Take a look at BOX/UNBOX, maybe you can make something with this.

DEFINE TEMP-TABLE tt NO-UNDO
    FIELD Integ3r   AS INT64
    FIELD Ch4racter AS CHARACTER
    FIELD D4teTim   AS DATETIME-TZ
    FIELD L0gical   AS LOGICAL
    FIELD Dec1mal   AS DECIMAL
    .

DEFINE VARIABLE h          AS HANDLE              NO-UNDO.
DEFINE VARIABLE oInteg3r   AS CLASS System.Object NO-UNDO.
DEFINE VARIABLE oCh4racter AS CLASS System.Object NO-UNDO.
DEFINE VARIABLE oD4teTim   AS CLASS System.Object NO-UNDO.
DEFINE VARIABLE oL0gical   AS CLASS System.Object NO-UNDO.
DEFINE VARIABLE oDec1mal   AS CLASS System.Object NO-UNDO.

ASSIGN h = BUFFER tt:HANDLE
       
       oInteg3r   = BOX(707)
       oCh4racter = BOX("Character")
       oD4teTim   = BOX(NOW)
       oL0gical   = BOX(TRUE)
       oDec1mal   = BOX(123.456)
       .

CREATE tt.

RUN object2type (h:BUFFER-FIELD("Integ3r"), oInteg3r).
RUN object2type (h:BUFFER-FIELD("Ch4racter"), oCh4racter).
RUN object2type (h:BUFFER-FIELD("D4teTim"), oD4teTim).
RUN object2type (h:BUFFER-FIELD("L0gical"), oL0gical).
RUN object2type (h:BUFFER-FIELD("Dec1mal"), oDec1mal).

MESSAGE tt.Integ3r   SKIP
        tt.Ch4racter SKIP
        tt.D4teTim   SKIP
        tt.L0gical   SKIP
        tt.Dec1mal   SKIP
    VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.

/****/
PROCEDURE object2type:
DEFINE INPUT PARAMETER phField  AS HANDLE        NO-UNDO.
DEFINE INPUT PARAMETER poObject AS System.Object NO-UNDO.

    ASSIGN phField:BUFFER-VALUE = UNBOX(poObject).

END PROCEDURE.

Posted by mdanwarh on 20-Aug-2018 01:22

Sure, I will try it and let you know. Thanks a lot !!!

Posted by Lieven De Foor on 23-Aug-2018 08:54

Too bad this won't work on Unix...

For that to work we would need BOX/UNBOX to/from Progress.Lang.Object...

You can add your vote for that feature request here: community.progress.com/.../boxingunboxing_of_abl_datatypes_tofrom_progresslangobject

This thread is closed