Deniss
Deniss

Reputation: 317

How to iterate through temp-table that is being accessed using buffer?

I have found an article, about accessing temp-table that is declared inside the class, from outside the class. I'm trying to use 2nd solution(using handles and buffer). And while it works(i can get 1st or last element from it), i can't find a way to iterate through full temp-table.

Thank you.

Upvotes: 2

Views: 2633

Answers (1)

Jensd
Jensd

Reputation: 8011

You could do it by creating a dynamic query and attaching it to the buffer handle:

Class2.cls looks exactly the same, if added it below for reference.

Test2.p is changed:

DEF VAR c-class2 AS Class2.

DEF VAR local-ttbuf-hdl AS HANDLE.
DEF VAR tt-def-buff AS HANDLE.

c-class2 = NEW Class2().
local-ttbuf-hdl = c-class2:GetTempTableHandle().
tt-def-buff = local-ttbuf-hdl:DEFAULT-BUFFER-HANDLE.


/*
/* Find last is commented out */
tt-def-buff:FIND-LAST().

MESSAGE tt-def-buff:BUFFER-FIELD(1):buffer-value SKIP
        tt-def-buff:BUFFER-FIELD(2):buffer-value
        VIEW-AS ALERT-BOX.

*/

/**** New code here ****/
/* Define and create a dynamic query */
DEFINE VARIABLE hQuery AS HANDLE      NO-UNDO.
CREATE QUERY hQuery.

/* Set the buffer */
hQuery:SET-BUFFERS(tt-def-buff).

/* Create a query-string */
hQuery:QUERY-PREPARE("FOR EACH " + tt-def-buff:NAME).

/* Open the query */
hQuery:QUERY-OPEN.


REPEAT :

    /* Get the next record, for the first run it will be the first record */
    hQuery:GET-NEXT().

    /* Leave the repeat if there are no more records */
    IF hQuery:QUERY-OFF-END THEN LEAVE.

    /* Display */
    MESSAGE tt-def-buff:BUFFER-FIELD(1):buffer-value SKIP
            tt-def-buff:BUFFER-FIELD(2):buffer-value
            VIEW-AS ALERT-BOX.

END.

/* All created objects should be deleted */
DELETE OBJECT hQuery.

Class2.cls:

CLASS class2:
   DEF VAR i AS INTEGER.
   DEF TEMP-TABLE tt
          FIELD i1 AS INTEGER
          FIELD c1 AS CHARACTER.

   CONSTRUCTOR class2():
        DO i = 1 TO 10:
             CREATE tt.
             ASSIGN
                 tt.i1 = i
                 tt.c1 = STRING(i).
        END.
   END CONSTRUCTOR.

   METHOD PUBLIC HANDLE GetTempTableHandle():
        RETURN TEMP-TABLE tt:HANDLE.
   END.
END CLASS.

Upvotes: 3

Related Questions