This has three uses. First, with help from the new keyword and the constructor for a type, it can allocate an array of memory. Second, it serves as a type modifier that turns any type into an array of that type. Third, through indexed and default properties, it can index an array or other type.
When you allocate an array, there are two different methods. Both allocate an array of a known size. However, one initialized the contents of the array to a default value for the type of the element. The other takes a comma delimited list of values to place in the array.
To create a blank array with the compiler-provided default value for each element, place the desired length of the array between the brackets. You can use this option even if you don't know the required length of the array until run-time.
\new\ /TypeDescriptor/\[\/ArrayLength/\]\
For more on what /TypeDescriptor/, see Type descriptors.
In this case, omit the length. The compiler will determine that from the number of passed elements. You must know the number of elements at compile-time with this option. However, the values to be placed in the elements don't have to be constant. When you use this version of the [] operator, you're calling the array constructor with the values to be placed into the elements as parameters to the constructor. Those parameters should be a comma delimited lists just after the [] operator. If needed, surround the list with parentheses.
\new\ /TypeDescriptor/\[\\]\ /CommaDelimitedListOfValuesToGoIntoTheArrayElements/
Arrays can be const data members, var variables, expression variables, or conditional variables. Arrays are normally by reference. If you need it to be by value, use byvalue and be very careful with it as you will use a lot of space with a byvalue array.
The following sample shows the declaration of an array of type int
. It then initializes it using the syntax shown above.
var int[] array = new int[] 5, 3, 7, 8, 3
Indexed properties come in two forms: normal and default. A normal indexed property must be referenced by name. A default property is always indexed and is simply the instance of the type itself. When you look up the value in an ASIL array, you're calling its default property.
In addition to arrays, you could use indexed properties to retrieve a character from a string. Or you could implement the put and get functions needed for a hash map. In the sample below, if you want to declare a default property, use the self keyword as the identifier of the property. (Note: This sample doesn't show the accessors. You need at least one accessor in each and every property.) For more on indexed properties and properties in general, see the section [Properties].
\property\ /TypeDescriptor/ /Identifier/
When you need to access an element in an array or any property, just use the same syntax you'd use in C++. This sample assumes the array
variable declared above. Array types are one-indexed as is the BASIC standard on which ASIL descended.
array[2] = array[5]
Wiki: Operators
Wiki: Properties
Wiki: Sets
Wiki: keywords-byvalue
Wiki: keywords-conditional
Wiki: keywords-const
Wiki: keywords-constructor
Wiki: keywords-expression
Wiki: keywords-new
Wiki: keywords-var