Announcement

Collapse
No announcement yet.

Hidden problem with REDIM ?

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

    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
    Nathan Maddox

    #2
    From the documentation:
    Error 9 will only be generated if you have specified #DEBUG ERROR ON.
    Rod
    In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

    Comment


      #3
      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
      Nathan Maddox

      Comment


        #4
        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.
        Forum: http://www.jose.it-berater.org/smfforum/index.php

        Comment


          #5
          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.
          Rod
          In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

          Comment


            #6
            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.

            Comment

            Working...
            X
            😀
            🥰
            🤢
            😎
            😡
            👍
            👎