Announcement

Collapse

Documentation

All current versions of the PowerBASIC documentation are available for download/viewing here:

PowerBASIC Links
See more
See less

Use of LBOUND() and UBOUND() with REDIM

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use of LBOUND() and UBOUND() with REDIM

    When a REDIM occurs, array bounds are initialised in the process. Consequntly you should not use LBOUND() or UBOUND() in the REDIM statement.
    Assign these values to variables prior to the REDIM and use the variables in the REDIM statement.

    Depending on the the values involved and whether using LBOUND(), UBOUND() or both you may get a number of different results, including
    • A compile time "Interenal Error"
    • A runtime "Out of Bounds" error
    • Incorrect resulting bounds
    • The desired result
    i.e.
    '
    Code:
    #COMPILE EXE
    #DIM ALL
    #DEBUG DISPLAY ON
    FUNCTION PBMAIN() AS LONG
       LOCAL lngLB, lngUB AS LONG
       DIM sArr(10 TO 20) AS STRING
       ' ...
       'save them now before the failing code below!!!
       lngLB = LBOUND(sArr())
       lngUB = UBOUND(sArr())
    
       'Problem!
       REDIM sARR(LBOUND(sArr()) TO UBOUND(sArr()) + 10)
        ? STR$(LBOUND(sArr())) & STR$(UBOUND(sArr()))
    
       'do this instead
       REDIM sArr(lngLB TO lngUB + 10)
       ? STR$(LBOUND(sArr())) & STR$(UBOUND(sArr()))
       '...
    END FUNCTION
    Substituting
    Code:
       REDIM PRESERVE sARR(LBOUND(sArr()) TO LBOUND(sArr())+ 10)
    in the code above correctly return "10 30"
    but substituting
    Code:
       REDIM PRESERVE sARR(LBOUND(sArr()) TO LBOUND(sArr())+ 21)
    returns "10 10"




  • #2
    Deleted

    Comment

    Working...
    X