|
-
Oct 3rd, 2000, 09:42 PM
#1
Thread Starter
Frenzied Member
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
-
Oct 3rd, 2000, 10:13 PM
#2
Hyperactive Member
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]
-
Oct 3rd, 2000, 10:14 PM
#3
Fanatic Member
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.
-
Oct 3rd, 2000, 10:29 PM
#4
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|