Redim preserve

 BBS: Inland Empire Archive
Date: 04-07-93 (21:22)             Number: 335
From: QUINN TYLER JACKSON          Refer#: NONE
  To: MICHAEL BULL                  Recvd: NO  
Subj: Redim preserve                 Conf: (2) Quik_Bas
MB>        Quinn, sorry for taking so long.  I sat down and had a good
MB>        crack at that code you sent this way, but can't get past the
MB>        Redim Preserve with my QB45.  I hope to be upgrading very soon
MB>        to VB, but till then, could you either explain Redim Preserve to
MB>        me so I can figure out what it is doing so I can fix it up to do
MB>        it, or post some code that handles?

Try this 'un, Michael:

' RedimPreserveXXX
' Written by Quinn Tyler Jackson 6 March 1993
' Here is an ugly, slow, but otherwise functional work around for
' the PDS/VBDOS REDIM PRESERVE feature.  Basically, you can redimension
' an array with this baby, without losing what is already stored within
' the array.  I've designed the SUB so that it can be EASILY rewritten
' to accomodate ANY data type, with a few alterations.
' To rewrite it to handle INTEGER types, for example, change the SUB
' name to RedimPreserveINT and change all cases of "AS STRING" to
' "AS INTEGER".  That's all that needs to be changed for it to handle
' INTEGERs.  All other datatypes, including user declared TYPEs can be
' accomodated similarly just by changing the three letter suffix and the
' AS declarations to suit the data type you want to be able to handle.
' Where you see REDIM PRESERVE SomeArray$(150) in PDS/VBDOS code,
' just substitute RedimPreserveStr SomeArray$(), 150.  It ALMOST looks
' the same.
' NOTE: This SUB can only act upon DYNAMIC arrays!
SUB RedimPreserveStr (Array() AS STRING, NewUpperLimit)

OldBottom = LBOUND(Array)
OldTop = UBOUND(Array)

IF NewUpperLimit = OldTop THEN EXIT SUB'since nothing needs to be done!

IF NewUpperLimit < OldTop THEN  ' we'll be shinking the array
    Top = NewUpperLimit
ELSE                            ' we'll be adding blank elements to the end
    Top = OldTop

DIM Temp(OldBottom TO Top) AS STRING

' First, we save the old array's data
FOR i = OldBottom TO Top
    Temp(i) = Array(i)

' Then, we REDIM using the old style QuickBASIC destructive REDIM

REDIM Array(OldBottom TO NewUpperLimit) AS STRING

' Finally, we restore the old data to the now empty array....

FOR i = OldBottom TO Top
    Array(i) = Temp(i)

' The next line isn't strictly necessary in a SUB, since all DYNAMIC
' arrays are zapped when we exit the SUB, but I'll add it for clarity.



 * OLX 2.1 TD * Pascal is some dead guy and ships sink in the C.

--- Maximus/2 2.01wb
 * Origin: The Nibble's Roost, New Westminster BC 604-526-7686 (1:153/918)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET