

' Now loop through array, adding subdirectory information. ' does not support it, so get array of files firstĭim vDir As Variant, vSubDir As Variant, i ' NB cannot use recursion immediately as Dir Public Function GetFiles(ByVal vPath As Variant) As Variant V = GetFiles("C:\") ' Places contents of C: into v In this piece of code, an entire directory structure is folded up and inserted in a single Variant: Private Sub Form_Load() This ability of Variants to hold arrays of Variants permits some interesting new data structures in Visual Basic. However you have to contrast that with the fact that the Variant "trees" are harder to set up. As you can see from the diagram, we do not have all the wasted space of a multidimensional array. These are sometimes known as ragged arrays.

Figure 4-2 explains the difference pictorially.įigure 4-2 The difference between a standard two-dimensional array (top) and a Variant array (bottom) The contained arrays-corresponding to the lower dimensions of a multidimensional array-do not have to have the same number of elements. How do these compare to more standard multidimensional arrays? Well, on the positive side, they are much more flexible. ' Make v an array of two Variants, each of which is an array
#Visual basic array vs arraylist code
This odd bit of code works fine: Dim v As Variant, i As Integer Just how deep can these arrays be nested? I don't know if there is a theoretical limit, but in practice I have tested at least 10 levels of nesting. So what is useful about placing an array in a Variant? As Variants can contain arrays, and they can be arrays of Variants, those contained Variants can themselves be arrays, maybe of Variants, which can also be arrays, and so on and so forth. Before version 6 you could utilize array copying only with Variant arrays, but now you can do this with any variable-sized array. What consequence does this have? Not much anymore. This code is creating an array of Variants, but this is not a Variant array. It can be easy to get confused here, look at the following: ReDim a(5) As Variant Notice that the only difference between the last two arrays is that one is a Variant holding an array of integers and the other is a Variant holding an array of Variants. ' From then on, ReDim Preserve will work on v Start off with Array, and then ReDim to preferred size ' Iterate adding elements using a normal array. Create a normal array, and assign it to a Variant. V = Array(0, 1, 2, 3) 'of little practical use ' Different ways to create Variant arrays The following code shows three ways of creating a Variant array of the numbers 0, 1, 2, 3: Dim v As Variant
#Visual basic array vs arraylist plus
The Array function, on the other hand, always creates an array of Variants- VarType 8204 (which is 8192 plus 12). The first of these methods creates a Variant whose subtype is the array value (8192) added to the value of the type of the original array. To set up a Variant to be an array, you can either assign it to an already existing array or use the Array function. The ability to store arrays in Variants was new to Visual Basic 4, and a number of new language elements were introduced to support them such as Array and IsArray.
