Array() Quickly Builds a one-Dimensional Array with Data


Use the Array function to quickly build a Variant array with data. The Array function returns a Variant array that contains the arguments to the function. See Listing 2 . This is an efficient method to create a Variant array containing a predefined set of values. One entry in the array is created for each argument.

Listing 2: The Array function returns a Variant array.
start example
 Dim vFirstNames                   'A Variant can reference an array Dim vAges()                       'A Variant array can reference an array vFirstNames = Array("Tom", "Rob") 'Array contains strings vAges = Array(18, "Ten")          'Array contains a number and a string Print vAges(0)                    'First element has the value 18 
end example
 

Use the Array function to quickly generate an array that already has data. The following code creates an array with five elements, zero through four, and then individually initializes each element.

 Dim v(4) v(0) = 2 : v(1) = "help": v(2) = Now : v(3) = True : v(4) = 3.5 

This can be done in a much simpler manner. Constants can be used as arguments to functions. You aren't always forced to assign a value to a variable so that you can call a function.

 Dim v() v() = Array(0, "help", Now, True, 3.5) Print Join(Array("help", 3, "Joe", Firstname)) 

The argument list is a comma-separated list of expressions, which can be of any type because each element in the returned array is a variant.

Variant variables can contain any type, including an array. I frequently use variant variables when I retrieve values from methods when I'm not certain of the return type. I then inspect the type and use it appropriately. This is a convenient use of the Variant variable type. Because a Variant variable can contain any type-including an array-each element in a Variant array can also contain an array. The code in Table 2 demonstrates placing an array inside another array. The code in each column is roughly equivalent.

Table 2: A Variant can contain an array, so these accomplish the same thing.
 Dim v(1) v(0) = Array(1, 2, 3) v(1) = Array("one", "two", "three") 
 Dim v() v = Array(Array(1, 2, 3),_      Array("one", "two", "three")) 

One advantage of Variant arrays is that it's possible to easily build collections of information of different types. For example, the item description (String), stock-tracking ID (Integer), and invoice amount (Double or Currency) can readily be stored as rows in an array, with each type of data stored in a single row for each customer. This allows array rows to behave more like rows in a database. Older programming languages required use of separately declared arrays for each data type, with added programming overhead of managing the use of multiple, related arrays of data.

Although it's good to understand how this works, this isn't the best way to create an array with two dimensions. It's cumbersome to address the elements of an array that is contained in another array. The only way to access the elements of an array contained in another array is to extract the contained array into a temporary variableand then access the appropriate element. See Listing 3 .

Listing 3: It's cumbersome to extract data from an array contained in another array.
start example
 Dim v () v = Array (Array (1, 2, 3), Array("one", "two", "three")) Dim x As Variant x = v(0)            'This is very cumbersome. print x(1)          'prints 2 
end example
 

Although it's easy to create an array inside of an array, it's typically easier to use an array with multiple dimensions, as shown in Listing 4 . The "array of arrays" construction is sometimes useful, if there is an obvious relationship with the natural organization of the data. Generally, it is best to select a way to organize the data that has the most direct, natural, and memorable relationship to how it is produced, used, and manipulated. OOo offers both options, giving programmers flexibility in designing OOo macro programs.

Listing 4: It is easier to use multi-dimensioned arrays than arrays inside of arrays.
start example
 Dim v(0 To 1, 0 To 2) v(0, 0) = 1     : v(0, 1) = 2       : v(0, 2) = 3 v(1, 0) = "one" : v(1, 1) = "two"   : v(1, 2) = "three" Print v(0, 1)   'prints 2 
end example
 

As of OOo version 1.1, a Variant array can be assigned to any other array, regardless of its declared type. Assigning one array to another causes one array to reference the other; they become the same array. As mentioned earlier, this is a bad idea. This is considered a bug and it may not be allowed in later versions of OOo Basic. Use the Array function and enjoy the flexibility, but assign the returned Variant array to either a Variant or a Variant array.

Tip  

Option Base 1 has no effect on the dimensions of the array returned by the Array function. The lower bound of the array is always zero.

As of OOo version 1.1.1, calling the Array function with no arguments returns an array with zero dimensions. The result is that IsArray(Array()) succeeds, but LBound(Array()) causes a run-time error. This behavior is scheduled to change in version 2.0.

Assigning a Variant array to variables declared as a non-Variant array is ill-advised. For example, after an Integer array has been assigned to reference a Variant array, it's possible to assign non-integer values to elements in the array. References to the array won't return the expected values due to the mismatch between Integer and Variant data types.




OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net