/---------------------\ | Notes for lecture05 | | 28 September 1999 | | CS 125-609 | | Michael Goldwasser | \---------------------/ ========================================================================= RESIZING AN ARRAY (using ReDim) Chapter 4.1 briefly discusses the use of ReDim, but let's look more carefully. -- "I want a global array, but I don't know the size at design time?!" Solution: At the top of your program code, type: Dim myarray() As Integer Then within a procedure, when you know what size it should be, type: ReDim myarray(1 to size) As Integer ------------------------------------------------------------------------- -- Dynamic arrays You can actually use ReDim to change the size of existing arrays. For instance, maybe you picked an array of a certain size, but later in the program you realize you need a bigger array. Alternatively, at one point your program may have needed a really big array, but now you only need a small array. If you tell this to the computer, then it can use the extra memory somewhere else! Syntax: ReDim myarray(20) ' lots of other instructions ReDim myarray(10) ' now the array of size 20 is throw away ' and you get an array of size 10 instead ------------------------------------------------------------------------- -- BEWARE If you type the above, all of the contents of the original array are lost!!!!! (even if you just made the array bigger) WHY???? Because the new array may need to be in a completely different place in the computer's memory. ------------------------------------------------------------------------- -- Preserving your data If you want to change the array by simply adding or deleting spaces at the end of your array, Visual Basic gives you a way. Syntax: ReDim myarray(20) ' lots of other instructions ReDim Preserve myarray(10) ' now the last 10 items are lost, but the rest remain HOW DOES THIS WORK??? it simply creates a brand new array, then copies over the relevent data to the new array before destroying the old. ------------------------------------------------------------------------- Note: You cannot use Preserve to add/remove items to/from the beginning of an array (works only at the end) ------------------------------------------------------------------------- Note: You cannot use ReDim to change the *type* of variables stored in the array. ========================================================================= MULTI-DIMENSIONAL ARRAYS If you want a two dimensional array (e.g., storing a matrix), Syntax, Dim Matrix(1 to 5, 1 to 10) Then you can refer to specific elements of this array such as, Matrix(3,6) = 4.58 Matrix(2,10) = 3.14159 ------------------------------------------------------------------------- If you want even more dimensions...go right ahead (assuming you can keep track of them!) The help pages say you can have up to 60 dimensions. ------------------------------------------------------------------------- - Using ReDim with multi-dimensional arrays: Yes, you can actually change the size of these arrays too, but in a very limited way. * You cannot change the number of dimensions * You can only resize the LAST dimension, and again, only by adding or deleting elements at the upper end. WHY??? [lesson on how these arrays are laid out in memory] ========================================================================= CONTROL ARRAYS - Gives a set of controls the same name (but gives each one a unique "Index" and its own set of properties) ------------------------------------------------------------------------- - Also allows you to easily specify many controls with similar or related properties (including the position/size of the control!) Lets you use loops to modify the properties of the controls. ------------------------------------------------------------------------- - Allows these controls to be handled by the SAME event procedures! - e.g., clicking on any one of them calls the same routine. You are told, however, the Index which belongs to the specific control which caused the event. This allows you to adjust your behavior in a patterned way. Syntax: Private Sub cmdarray_Click(Index As Integer) End Sub ------------------------------------------------------------------------- - Note: You do not actually need to specify a continuous list of indices. That is, you can choose to define a control array which contains only indices {1,2,5,8} if that is what you want. (with standard arrays, you had to ask for a continuous chunk, such as "1 to 8") ------------------------------------------------------------------------- - Note: *** you cannot have multi-dimensional control arrays *** ------------------------------------------------------------------------- - You can create control arrays either at design time or at run time. - At design time: there are several ways to add items to an control array. (1) Create a new control and assign it the same name as some other control already there. Visual Basic will ask you, "You already have a control named 'blah'. Do you want to create a control array?" (2) "Copy" an existing control and then "Paste" it. Again, Visual Basic will ask you if you want to create a control array. (3) Take an existing control, and set it's "Index" property to any non-negative integer. (you have just created an array with only one control, but you can add more now or at runtime!) - At run time: You cannot actually create a control array at runtime, you can only add/delete from an existing control array. For this reason, you must set up a control array at design time, even if it only holds one item. (of course you can always make that one object invisible if you really don't want it on the screen at startup) - To add to an existing array, Syntax: Load cmdarray(5) ' adjust any properties you want to modify cmdarray(5).Visible = True The property settings for the new control are copied from the lowest indexed member of the control array (e.g., cmdarray(0)). However, the Visible property is NOT copied, it is always False until you explicitly set it to True. - To delete a member of a control array: (this ONLY works for controls which were added with Load) Syntax: Unload cmdarray(5) =========================================================================