Creating .NET Arrays in Xbasic
Description
This section discusses creating .Net Array objects from Xbasic.
Discussion
While Xbasic will automatically convert built-in Xbasic array types into equivalent .Net, there are conditions where it is required that .Net arrays be explicitly created.
A good example of where explicitly created .Net array objects are needed is with the many 'value' types. In Xbasic, numeric values are generally always floating point. When we convert a numeric array to DotNot implicitly on invocation, the .Net type is always created as an array of Double values.
If the .Net property you are setting, or method you are calling expects an array of 32 Bit integer values (Int32) then passing an array of Double will result in a type error, and the call will fail.
Creating and Assigning an Array of Int32 types from Xbasic
When the type specified in an Xbasic array dim statement is a .Net type, the array that is created will be a .Net array type.
Note that unlike Xbasic arrays, .Net arrays are always a fixed size, so you need to know the number of elements up front.
Note that unlike C#, the array operator in Xbasic starts at '1' not '0'.
dim arr[2] as System::Int32 arr[1] = 100 arr[2] = 200
Creating and Multidimension Arrays
The following is an example of a complex array with multiple dimensions.
Note that Xbasic always adds one to the index, so the index column and row '2,3' from Xbasic is '1,2' in .Net.
dim arr[3,4] as system::Int32 ? arr.GetLength(0) = 3 ? arr.GetLength(1) = 4 arr[1,1] = 101 arr[1,2] = 102 arr[1,3] = 103 arr[1,4] = 104 arr[2,1] = 201 arr[2,2] = 202 arr[2,3] = 203 arr[2,4] = 204 arr[3,1] = 301 arr[3,2] = 302 arr[3,3] = 303 arr[3,4] = 304 ? arr[2,3] = 203 ? arr.Get(1,2) = 203
Creating and Arrays with different Bounds
Bounds can be specified in the Xbasic DIM statement, but like the default '0' lower bound, all the bounds are adjusted by one for Xbasic.
Note that lower bounds returned for first and second dimension are 1 and 100, even though we specified 2 and 101. The array entry '2,102' is actually '1,101' in .Net.
dim arr[2..4,101..102] as system::Int32 ? arr.GetLength(0) = 3 ? arr.GetLength(1) = 2 ? arr.GetLowerBound(0) = 1 ? arr.GetLowerBound(1) = 100 arr[2,101] = 1 arr[3,101] = 2 arr[4,101] = 3 arr[2,102] = 4 arr[3,102] = 5 arr[4,102] = 6 ? arr[2,102] = 4 ? arr.Get(1,101) = 4