Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > 'ReDim Preserve' problem


Reply
 
Thread Tools Display Modes
  #1  
Old 02-12-2004, 06:17 AM
natrajv's Avatar
natrajv natrajv is offline
Centurion
 
Join Date: Sep 2003
Posts: 163
Default '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?
Reply With Quote
  #2  
Old 02-12-2004, 06:18 AM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

You can only resize the last dimension of a dynamic array when specifying Preserve.

This is all explained in your MSDN library.
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #3  
Old 02-12-2004, 06:20 AM
natrajv's Avatar
natrajv natrajv is offline
Centurion
 
Join Date: Sep 2003
Posts: 163
Default

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.
Reply With Quote
  #4  
Old 02-12-2004, 06:25 AM
wilbert's Avatar
wilbert wilbert is offline
Senior Contributor
 
Join Date: Dec 2002
Location: Netherlands
Posts: 867
Default

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
Reply With Quote
  #5  
Old 02-12-2004, 06:28 AM
natrajv's Avatar
natrajv natrajv is offline
Centurion
 
Join Date: Sep 2003
Posts: 163
Default

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?
Reply With Quote
  #6  
Old 02-12-2004, 06:30 AM
wilbert's Avatar
wilbert wilbert is offline
Senior Contributor
 
Join Date: Dec 2002
Location: Netherlands
Posts: 867
Default

Like I said in my thread.... ... but it's not very effective

Wilbert
Reply With Quote
  #7  
Old 02-12-2004, 06:33 AM
natrajv's Avatar
natrajv natrajv is offline
Centurion
 
Join Date: Sep 2003
Posts: 163
Default

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.
Reply With Quote
  #8  
Old 02-12-2004, 07:05 AM
John's Avatar
John John is offline
Bit Flipper
 
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
Default

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.
__________________
Subclassing|Magnetic Forms|Operator Overloading (VB2K5)|QuickSnip.NET

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Reply With Quote
  #9  
Old 02-12-2004, 08:14 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,918
Default

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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Redim Preserve doesn's work?!?! Masterwiz General 3 12-22-2003 01:19 PM
Redim Preserve Statement... Rookie General 1 09-25-2003 12:15 AM
ReDim Preserve (subscript out of range) Virgo Game Programming 4 11-18-2002 04:43 PM
Redim Preserve Problems davie General 7 09-12-2002 06:19 AM
Using Redim Preserve to change first dimension ezyeric General 1 10-02-2001 11:17 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->