ReDim
ReDim ArrayName(ArraySize)
 
Parameters: NONE
Returns: NONE
 

      The ReDim command works virtually the same as the Dim command, with one key difference. ReDim allows the programmer to resize a previously declared array without destorying it's contents.

      This allows us to dynamically expand or shrink arrays as need be. However you should be aware that shrinking an array will discard any infromation outside of the new dimensions. So if that information is important to you, it will be lost. So some care is needed.



FACTS:


      * ReDim can be used with any type of dynamic array, including Integer(),Float#(), String$() and even Typed Variables & Arrays (Types).

      * Array dimensions must be positive values.

      * Array Indexes are zero inclusive. That means 0,1,2, etc .. up to the Number of Elements are valid Indexes. Which is the traditional BASIC convention.

      * Arrays can be as large as you like, providing it's total size (in all dimensions) is smaller than (2^28), which is massive anyway. So you're really only limited by the amount of the memory your computer has.

      * You can not change the number of dimensions that the array previously had.





Mini Tutorial:


      In this example were creating an Array with DIM, then resizing it using the REDIM command.

  
; Use DIM to Create an array called Highscores.
  Dim HighScores(0)
  
; Randomly pick a size for for this array , between 1 and 20 elements.
  size=RndRange(1,20)
  
; Now Use ReDim to Size the Array Ready For actual use.
  ReDim HighScores(Size)
  
; Fill the Array with Values.
  For lp=1 To size
     HighScores(lp)=lp*1000
  Next
  
; Display The array To the Screen.
  For lp=1 To size
     Print HighScores(lp)
  Next
  
; Display the Screen and wait for a key to be pressed.
  Sync
  WaitKey
  
  



Since ReDim gives us easy control over an arrays size. We'll commonly use when we wish to dynamicly change the size of an array.

In this example, we're creating the array table(), then randomly adding values to it, resizing it when need be.


  
; Create the Array
  Dim Table(0)
  
; place a Random Number of values into an array called TABLE()
  For lp=1 To RndRange(1,20)
     
   ; Check the SIze of the TABLE() Array.  If it's smaller than needed, then resize it.
     
     If GetArrayElements(Table(),1< lp
        
      ; resize the Table array
        ReDim Table(lp)
        
     EndIf
     Table(lp) = Rnd(1000)
  Next lp
  
  
; Display this Array to the screen
  For lp=1 To GetArrayElements(Table(),1)
     Print Table(lp)
  Next lp
  
; Display the Screen and wait for a key to be pressed.
  Sync
  WaitKey
  
  


 
Example Source: Download This Example
; ================================
; REDIM EXAMPLE by Kevin Picone
; ================================
  
  Size=10
  
; Create an Integer Array called "MyArray" containing 10 elements
  Dim MyArray(Size)
  
; Fill MyArray() with some random values between 0 and 1000
  For lp=0 To SIze
     MyArray(lp)=Rnd(1000)
  Next lp
  
; Call our function To Display the Array Contents across the Screen.
  ShowMyArray(Size)
  
; Add 10 to our array Size variable
  Size=Size+10
  
; now ReDIM (ReSize) MyArray() without destorying it's previous contents..
  ReDim MyArray(Size)
  
; display a message to the users
  Print "Contents of MyArray()after being resized with Redim"
  
; Call our function To Display the Array Contents  across the Screen.
  ShowMyArray(Size)
  
  
  
; Display the Screen and and wait for input
  Print "Press any Key To End"
  Sync
  WaitKey
  
; end this program
  End
  
  
  
Function ShowMyArray(Size)
  Print ""
  Print "Displaying the Contents of MYARRAY()"
  Print "------------------------------------"
  For lp =0 To size
     Contents$=Contents$+Str$(MyArray(lp))
     If lp<size Then Contents$=Contents$+","
  Next
  Print Contents$
  Print ""
EndFunction
  
 
Related Info: ArrayBasics | Dim | GetArrayElements | GetArrayStatus | UnDim :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com