Announcement

Collapse
No announcement yet.

Hidden problem with REDIM ?

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

  • Chris Holbrook
    replied
    If you want to loop through a whole array, then

    Code:
        for i = lbound(arr()) to ubound(arr()) 
            ....
        next
    is safest, as the code does not need to know what the array bounds are, which means that the actual array bounds are only specified in DIM or REDIM statements.

    Leave a comment:


  • Rodney Hicks
    replied
    The error text is terse when you first encounter it. I've found it helps to check out the error in the documentation, just to see what surprises are lurking there. I would think even more so when you don't encounter it and you think you should.

    Leave a comment:


  • José Roca
    replied
    REDIM Temp$(2) '-----2 NOT 3 --------
    DIM and REDIM use a lower bound of 0 if you don't specify it. Therefore, with REDIM Temp$(2) you get an array of 3 elements (from 0 to 2). In BASIC, the "(2)" in your statement specifies the upper bound of the array, not the number of elements.

    Leave a comment:


  • Nathan Maddox
    replied
    Ouch.

    Thank you.

    I looked for something like that in REDIM's documentation and it wasn't there.

    Didn't occur to look elsewhere. (There's no other See also)

    Thanks

    Nathan

    Leave a comment:


  • Rodney Hicks
    replied
    From the documentation:
    Error 9 will only be generated if you have specified #DEBUG ERROR ON.

    Leave a comment:


  • Nathan Maddox
    started a topic Hidden problem with REDIM ?

    Hidden problem with REDIM ?

    or just undocumented feature?

    I would expect 2 subscript out of ranges (Error 9) on this code, but with
    PB 9.0 I do not get any....

    What do you think?

    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
        LOCAL I AS LONG
        LOCAL Temp$()
    
        REDIM Temp$(2)                                 '-----2 NOT 3 --------
        Temp$(1)="HELLO"
        Temp$(2)="PowerBasic"
        Temp$(3)="Hero"                                 '---Expect Subscript out of range
        IF ERR THEN ? "Err="+STR$(ERRCLEAR)
        
        FOR I=1 TO 3
            ? "Temp$("+STR$(i)+")="+Temp$(I) '--- Expect Error 9 on 3rd iteration
        NEXT
        IF ERR THEN ? "Err="+STR$(ERRCLEAR)
    
    END FUNCTION
Working...
X