Announcement

Collapse
No announcement yet.

Redim preserve

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

  • Redim preserve

    Can someone help me with REDIM with the PRESERVE option. The online help says the following, if you REDIM PRESERVE an array with 10 elements to 20 elements, the first 10 elements will retain their original value. The remaining 10 elements will be initialized to zero(or null/empty in the case of a string array). First off EXACTLY how would one do the syntax for doing this. Second does this mean that if you have an Array filled with fields from a Random Access file that you can REDIM PRESERVE the array with one more element by getting the UBOUND of the Array and adding one to the result. I tried to do this by doing the following. nRecs& = UBOUND(myArray()) then REDIM PRESERVE myArray(1 TO nRecs& + 1), but this does not work so can someone show me how to use the REDIM PRESERVE.

    Thanks in advance
    Leroy Hindman

  • #2
    I can't see any error so guessing the array dimensions weren't what you expected.
    Check that the array wasn't DIMed twice (second dim will fail.)
    Be sure it isn't a global array and also a local array.
    Check that the LBOUND is 1.
    Check that the array wasn't accidentally dimed with more than 1 dimension.

    Code:
    FUNCTION PBMAIN () AS LONG
     DIM s(1 TO 1) AS STRING
     s(1) = "One"
     REDIM PRESERVE s(1 TO 2)
     s(2) = "Two"
     ? JOIN$(s(),$CR)
    END FUNCTION
    https://duckduckgo.com instead of google

    Comment


    • #3
      I seem to recall that with REDIM Preserve you cannot change the lower bound. Check it out in Help.

      When PRESERVE is specified, you can resize only the upper boundary of the last (outer) dimension of the array

      Comment


      • #4
        Gary:
        It does say you can resize only the upper boundary of the last (outer) dimension of the array. Does this mean if you have an Array(1 TO 100, 1 TO 12) and I use the REDIM PRESERVE I can only redimension the 12?

        Comment


        • #5
          That is true
          Code:
          FUNCTION PBMAIN () AS LONG
           DIM s(1 TO 1, 1 TO 1) AS STRING
           REDIM PRESERVE s(1 TO 1, 1 TO 2) AS STRING
           ? JOIN$(s(),$CR),,USING$("cols #  rows #",UBOUND(s),UBOUND(s,2))
          END FUNCTION
          https://duckduckgo.com instead of google

          Comment


          • #6
            As others have indicated, if "REDIM PRESERVE myArray(1 TO nRecs& + 1)" isn't working, it is probable that myArray() was not originally DIMed with a LBOUND of 1.

            Note that if you DIM(10), you get 11 items from 0 to 10, not 10 items from 1 to 10.

            Comment

            Working...
            X