 |

02-12-2004, 05:17 AM
|
 |
Centurion
|
|
Join Date: Sep 2003
Posts: 163
|
|
'ReDim Preserve' problem
|
ReDim Preserve not working for Two-dimensional
Dim aa() as Integer
ReDim aa(3,4) '--No error
ReDim aa(4,4) '--No error
'--Following statement raises error
'--Subscript out of range
ReDim Preserve aa(5,4)
I have no clue why it is happening only when I use
ReDim Preserve and not ReDim...
Any help?
|
|

02-12-2004, 05:18 AM
|
 |
Political Coder
Retired Moderator * Guru *
|
|
Join Date: Mar 2001
Location: London, England
Posts: 8,037
|
|
|
You can only resize the last dimension of a dynamic array when specifying Preserve.
This is all explained in your MSDN library.
|
|

02-12-2004, 05:20 AM
|
 |
Centurion
|
|
Join Date: Sep 2003
Posts: 163
|
|
Quote: Originally Posted by Squirm You can only resize the last dimension of a dynamic array.
Thanks... but, it works for ReDim and not for ReDim Preserve. Why this preculiar behaviour.
|
|

02-12-2004, 05:25 AM
|
 |
Senior Contributor
|
|
Join Date: Dec 2002
Location: Netherlands
Posts: 867
|
|
You can resize both elements of an array, but it is a bit unhandy... I use the following script, don't know if that's the most simplistic way doing it... but it works
Code:
Dim a() As Integer
Dim b() As Integer
Private Sub Change_Array_Size()
Dim X As Integer, Y As Integer
ReDim a(1 To 3, 1 To 3)
'Make a parallel array
ReDim b(LBound(a, 1) To UBound(a, 1), LBound(a, 2) To UBound(a, 2))
'Put all the info in that array
For X = LBound(a, 1) To UBound(a, 1)
For Y = LBound(a, 2) To UBound(a, 2)
b(X, Y) = a(X, Y)
Next Y
Next X
'Erase the array
Erase a()
'Redim the array
ReDim a(1 To 5, 1 To 5)
'Put all the info back in the redimmed array
For X = LBound(b, 1) To UBound(b, 1)
For Y = LBound(b, 2) To UBound(b, 2)
a(X, Y) = b(X, Y)
Next Y
Next X
End Sub
|
|

02-12-2004, 05:28 AM
|
 |
Centurion
|
|
Join Date: Sep 2003
Posts: 163
|
|
Quote: Originally Posted by Squirm You replied a little too quickly. Reread my post.
Sorry... but, I am not able to understand.
How to solve this?
Dim aa() as integer
for i=0 to 5
ReDim Preserve (i,4)
next
How I can dynamically alter two-dimensional array, without losing any previous contents...
presently I managed to do this by using Type declaration. But, I want to know is it possible with a simple two-dimensional array?
|
|

02-12-2004, 05:30 AM
|
 |
Senior Contributor
|
|
Join Date: Dec 2002
Location: Netherlands
Posts: 867
|
|
Like I said in my thread....  ... but it's not very effective
Wilbert
|
|

02-12-2004, 05:33 AM
|
 |
Centurion
|
|
Join Date: Sep 2003
Posts: 163
|
|
Quote: Originally Posted by wilbert You can resize both elements of an array, but it is a bit unhandy... I use the following script, don't know if that's the most simplistic way doing it... but it works 
Code:
Dim a() As Integer
Dim b() As Integer
Private Sub Change_Array_Size()
Dim X As Integer, Y As Integer
ReDim a(1 To 3, 1 To 3)
'Make a parallel array
ReDim b(LBound(a, 1) To UBound(a, 1), LBound(a, 2) To UBound(a, 2))
'Put all the info in that array
For X = LBound(a, 1) To UBound(a, 1)
For Y = LBound(a, 2) To UBound(a, 2)
b(X, Y) = a(X, Y)
Next Y
Next X
'Erase the array
Erase a()
'Redim the array
ReDim a(1 To 5, 1 To 5)
'Put all the info back in the redimmed array
For X = LBound(b, 1) To UBound(b, 1)
For Y = LBound(b, 2) To UBound(b, 2)
a(X, Y) = b(X, Y)
Next Y
Next X
End Sub
Seems to be a nice solution... but, I think there could be a simple solution lies somewhere...Anyway I took your script.(any copyright required...!!!)
Thanks.
|
|

02-12-2004, 06:05 AM
|
 |
Bit Flipper
|
|
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
|
|
Quote: Originally Posted by natrajv ...How I can dynamically alter two-dimensional array, without losing any previous contents...
presently I managed to do this by using Type declaration. But, I want to know is it possible with a simple two-dimensional array?
As Squirm already pointed out, you can only alter the size of the last element of a two dimensional array.
As he also said, it is all very well documented in your MSDN library. Just type click on redim and press F1.
|
|

02-12-2004, 07:14 AM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
Just change your logic to use the last dimension as the variable one
(assuming you don't need to change both). So
Code:
Dim aa() as integer
for i=0 to 5
ReDim Preserve (4, i)
next
would work fine.
If you change the last dimension, you are essentially just extending the
array in memory, so the previous contents don't have to be moved
relatively, so can be preserved easily.
If you change an earlier dimension, then that is breaking the array up
and inserting "chunks of data" at the end of every previous chunk of
array based on the earlier dimension sizes. This is a considerable
modification, since nearly all the data in the array has to be shuffled
around to allow the insertion of space into the array. So Visual Basic
does not support preserving data in an array when you need to
completely restructure the array like that.
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|