|
The situation is handled differently when there are arrays. An array can be seen as a set of elements where each one of them is identify be a name (the same for all) and an index. This is not intended to be a deep mathematical discussion. We will try to explain some principles of the subject in a very simple way.
So, an array can be seen as a group of elements with the same name. In order to access a specific element, it is necessary to know its index which will identify it uniquely.
In this Programming language an array is treated as any other variable, the only difference is the use of the index to identify the element to use.
An array must be declared as such before it can be used, and the syntax is very similar to the one used for the other variables:
Dim "array name" As "array type"() = "default"
Let's see and example:
Dim MyArray As Numeric() = 10
The only difference compared with simple variables is the use of parenthesis "()" after the variable type declaration. This simple symbol will tell the command interpreter that the variable being declared has to be treated as an array.
In this example the default value is "10". This means that all elements value will have value of 10 if no otherwise specified.
NB An array is initialized the first time anyone of its elements is used. If it is never used, it will not be initialized. The reason as something to do with the dynamic dimensioning of array.
In many programming languages when declaring the arrays it is necessary to specify also the number of element they will have.
Not here, the dimensioning will happen automatically. If you want to access the element in the position 16 and assign to it a value of 9, there is no need to actually make sure the array has 16 elements, it would be enough to write the following :
MyArray(16) = 9
It will be the command interpreter to size correctly the array to have 16 elements.
NB Of course the index of an array cannot be a negative number. If for any reason the index is negative, is will be set automatically to zero. Therefore MyArray(-1) will be interpreted as MyArray(0).
Very Important:
A function can return an array result. There is only one thing to keep in mind. It as to be told to the interpreter, the result is going to be an array.
The symbol "()" it is used after the indication of the function type:
Function "function name" ( "arg 1" as "arg type" , "arg 2" as "arg type" , ...) as "function Type" ()
Let's see and example:
Function MC (PP As Numeric, UP As Numeric) As Numeric()
This function could be give the value of an array in the calling function:
MyArray() = MC(1, 10)
Notes on valorization between arrays:
To an operation like the following :
MyArray1() = MyArray2()
there are a few things to take into account. Let's suppose that the declaration of MyArray2() was done as follows:
Dim MyArray2 As Numeric() = 10
If the operation of assigning MyArray2() to MyArray1() is done before any other use of MyArray2() then MyArray1() is an empty array with no elements. This because the command interpreter has never accessed MyArray2() and did not initialized it or dimensioned it. In case of a situation like the one above, there is the need to initialize MyArray2() unless the aim is to assign MyArray1() to an empty array.
|