Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bug when redimensioning multi-dimension arrays?
#24
I'm thinking this should now resize on both dimentsions, up or down, while preserving the original data:

Code: (Select All)
'Expanded upon by Steve-the-AMAZING(tm) from what bplus had originally  Wink

REDIM Grid(1 TO 5, 1 TO 5) AS LONG
' LoadInitialValues
FOR y = 1 TO 5
    FOR x = 1 TO 5
        counter = counter + 1
        Grid(x, y) = counter
    NEXT
NEXT
ShowArrayValues Grid()
Redim2D Grid(), 0, 6, 3, 4

ShowArrayValues Grid()
END

SUB Redim2D (arr() AS LONG, xlb AS LONG, xub, ylb AS LONG, yub AS LONG) ' xpr x per row
    DIM AS LONG lbx, lby, ubx, uby
    lbx = LBOUND(arr, 1)
    lby = LBOUND(arr, 2)
    ubx = UBOUND(arr, 1)
    uby = UBOUND(arr, 2)
    DIM c(lbx TO ubx, lby TO uby) AS LONG
    FOR y = lby TO uby
        FOR x = lbx TO uby
            c(x, y) = arr(x, y)
        NEXT
    NEXT
    REDIM arr(xlb TO xub, ylb TO yub) ' fixed
    'ReDim _Preserve arr(xpr, r)  'nope
    'ReDim _Preserve arr(lbx To xpr, lby To r)
    'ReDim _Preserve arr(lbx To xpr, lby To r) As Long ' nope
    FOR y = lby TO uby
        IF y >= ylb AND y <= yub THEN
            FOR x = lbx TO uby
                IF x >= xlb AND x <= xub THEN arr(x, y) = c(x, y)
            NEXT
        END IF
    NEXT
END SUB

SUB ShowArrayValues (arr() AS LONG)
    PRINT "Array Values:": PRINT
    FOR y = LBOUND(arr, 2) TO UBOUND(arr, 2)
        PRINT USING "Row ##  "; y,
        FOR x = LBOUND(arr, 1) TO UBOUND(arr, 1)
            PRINT USING " ##"; arr(x, y);
        NEXT
        PRINT
    NEXT
    PRINT
END SUB
Reply


Messages In This Thread
RE: Bug when redimensioning multi-dimension arrays? - by SMcNeill - 06-20-2024, 06:07 PM



Users browsing this thread: 4 Guest(s)