' COM-style enumeration simulation
#COMPILE EXE
#DIM ALL

GLOBAL gEnumCount AS LONG
GLOBAL gEnumPos AS LONG
GLOBAL gId0 AS LONG: GLOBAL gId1 AS LONG: GLOBAL gId2 AS LONG: GLOBAL gId3 AS LONG: GLOBAL gId4 AS LONG
GLOBAL gName0 AS STRING: GLOBAL gName1 AS STRING: GLOBAL gName2 AS STRING: GLOBAL gName3 AS STRING: GLOBAL gName4 AS STRING
GLOBAL gVal0 AS STRING: GLOBAL gVal1 AS STRING: GLOBAL gVal2 AS STRING: GLOBAL gVal3 AS STRING: GLOBAL gVal4 AS STRING

SUB InitEnum()
    gEnumCount = 5
    gEnumPos = 0
    gId0 = 1: gName0 = "First": gVal0 = "Value1"
    gId1 = 2: gName1 = "Second": gVal1 = "Value2"
    gId2 = 3: gName2 = "Third": gVal2 = "Value3"
    gId3 = 4: gName3 = "Fourth": gVal3 = "Value4"
    gId4 = 5: gName4 = "Fifth": gVal4 = "Value5"
END SUB

FUNCTION EnumNext(BYREF outId AS LONG, BYREF outName AS STRING, BYREF outVal AS STRING) AS LONG
    IF gEnumPos >= gEnumCount THEN FUNCTION = 0: EXIT FUNCTION
    IF gEnumPos = 0 THEN outId = gId0: outName = gName0: outVal = gVal0
    IF gEnumPos = 1 THEN outId = gId1: outName = gName1: outVal = gVal1
    IF gEnumPos = 2 THEN outId = gId2: outName = gName2: outVal = gVal2
    IF gEnumPos = 3 THEN outId = gId3: outName = gName3: outVal = gVal3
    IF gEnumPos = 4 THEN outId = gId4: outName = gName4: outVal = gVal4
    gEnumPos = gEnumPos + 1
    FUNCTION = 1
END FUNCTION

SUB ResetEnum()
    gEnumPos = 0
END SUB

SUB IterateEnum()
    LOCAL tmpId AS LONG
    LOCAL tmpName AS STRING
    LOCAL tmpVal AS STRING
    PRINT "Enumeration contents:"
    ResetEnum()
    DO
        IF EnumNext(tmpId, tmpName, tmpVal) = 0 THEN EXIT DO
        PRINT "  id="; tmpId; " name="; tmpName; " value="; tmpVal
    LOOP
    PRINT "  End of enumeration"
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== COM Enum Demo ==="
    PRINT "COM: IEnumX pattern iteration"
    InitEnum()
    IterateEnum()
    PRINT "=== End COM Enum ==="
    FUNCTION = 0
END FUNCTION