Results 1 to 4 of 4

Thread: redim preserve multidimensional array problem..

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I'm trying to ReDim and Preserve a dynamic mulitidimensional array but it's keeps giving me the subscript out of range error.

    It is a 2 dimensional array. I now the size of the 2nd dimension but not the 1st. So I'm trying to redim it as follows:

    Code:
    Dim sArray() as String
    Dim nCnt as Integer
    Dim nSub as Integer
    
    For nSub = 1 To ListView1.ListItems.Count
       ReDim Preserve sArray(nCnt, 3)
       sArray(nCnt, 0) = ListView1.ListItems(nSub).Key
       sArray(nCnt, 1) = ListView1.ListItems(nSub).SubItems(1)
       ...
       nCnt = nCnt + 1
    Next
    The above code works fine when I "pre" dimension the multidimensional array but as I said earlier, I need to dynamically redim the first dimension based on how many items are currently selected..

    I just read in MSDN, that "Only the upper bound of the last dimension can be changed when you use the Preserve keyword." Is that the problem I'm running into? Is it not possible to ReDim and Preserve the 1st dimension in a multidimensional array?

    Any help would be appreciated..

    Dan

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Arrays go from 0 to x - 1
    Collections go from 1 to x

    All you need to do is change your arrays to "nCnt - 1" and it will work perfectly.

    Code:
    Dim sArray() as String
    Dim nCnt as Integer
    Dim nSub as Integer
    
    For nSub = 1 To ListView1.ListItems.Count
       If nCnt > 0 then
           ReDim Preserve sArray(nCnt-1, 3)
       End If
       sArray(nCnt, 0) = ListView1.ListItems(nSub).Key
       sArray(nCnt, 1) = ListView1.ListItems(nSub).SubItems(1)
       ...
       nCnt = nCnt + 1
    Next
    [Edited by Gen-X on 10-03-2000 at 11:15 PM]

  3. #3
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    I ran into this problem myself a while back. The answer is pretty simple. Reverse the dimensions in the array. It sounds kinda wacky at first, but it will work the same way for the most part.

    The reason you have to do it this way is due to a limitation in VB, you can only redim the last part of the array, the right-most dimension.

    Code:
    Dim sArray() As String
    Dim nCnt As Integer
    Dim nSub As Integer
    
    For nSub = 1 to ListView1.ListItems.Count
        ReDim Preserve sArray(3, nCnt)
        sArray(0, nCnt) = ListView1.ListItems(nSub).Key
        sArray(1, nCnt) = ListView1.ListItems(nSub).SubItems(1)
        ...
        nCnt = nCnt + 1
    Next nSub
    This should give you the same functionality, it'll just be backwards from what you're used to in a multi-dimensional array ... It works though.
    -Excalibur

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Redimming MultiDimensional Arrays

    Just to add to the general discussion that should you need to redimension and array on more than one dimension, you are able to emulate this using CopyMemory.

    If you need an example I can write one. Otherwise I'll leave it to your imagination

    Cheers
    Paul Lewis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width