Array

From Real Software Documentation

Jump to: navigation, search
Language Keyword

Assigns a list of values to consecutive elements of a one-dimensional array.


Syntax

result = Array(elementList)

Part Type Description
result Any valid datatype for an array Name of the array that is populated with the items in ElementList.
ElementList Type of result A comma-delimited list of values that are used to populate result.

The data type of the items in ElementList should agree with the data type of the array result. If the items in ElementList are of different data types, such as Int32, UInt32, Int64 and so forth, Array will try to choose the data type that can accommodate all the elements passed to it. The data type of the array that receives the elements must agree with this data type. Otherwise, you will get a Type Mismatch error. See the examples in Notes.


Notes

The Array function provides the same functionality as separate assignment statements for each element of an array, beginning with element zero and proceeding consecutively. If there are more elements in the array than items in the list of elements, then the “extra” elements are not disturbed.

NOTE: Only one-dimensional arrays are supported by Array. Higher dimensional arrays can be populated via assignment statements.

If the ElementList contains numeric data of different word lengths and/or a mixture of signed and unsigned integers, Array will use the lowest common data type that accommodates all the elements. This may break code that worked prior to the introduction of signed/unsigned integers and integers of different word lengths. For example, in versions of Real Studio prior to 2006, the statement:

Array(&h01, &h02 )

returned an array of integers.

Because hexadecimal numbers are unsigned integers, this expression now returns an array of UInt32s. This means that the following code will generate a Type Mismatch Error:

Dim i() as Integer
i = Array(&h1, &h2)

You need to be careful about word length and whether or not the integer is signed. In this instance, the following will compile:

Dim i() as UInt32
i = Array(&h1, &h2)


If you wish, you can also decide to convert one of the values to a signed integer. In that case, Array will return an Int32 (a.k.a., Integer) array. Use either of the following ways:

Dim i() as Integer
i = Array(Int32(&h1),&h2)


Or this:

Dim i() as Integer
i = Array(1,&h2)


2D arrays

2D arrays require some special treatment, and most of the usual array functions are not supported.


To create a 2D array you use

Dim My2Darray( -1,-1 ) As String


You cannot dim an array with a variable, but you can do the following:

Dim NumberOfRows As Integer = someOtherArray.Ubound
Dim My2Darray( -1,-1 ) As String
ReDim My2Darray(NumberOfRows, 9)


To pass a 2D array to a method you use the following in the method declaration

Sub MyMethod(param(,) As String)

Examples

The following statements initialize the array using separate assignment statements for each array element:

Dim names(2) As String
//using separate assignment statements
names(0) = "Fred"
names(1) = "Ginger"
names(2) = "Stanley"

The following statements use the Array function to accomplish the same thing. Note that you don’t have to declare the exact size of the array in the Dim statement. The Array function will add elements to the array as needed.

The following statement creates and populates the first three elements of the array aNames.

Dim names() As String
// using the Array function
names = Array("Fred", "Ginger", "Stanley")

See Also

Dim statement; Join, Split, Ubound functions; Append, IndexOf, Insert, Pop, Redim, Remove, Shuffle, Sort, Sortwith methods; ParamArray keyword.

Personal tools