With Option Strict On
, although the .NET Framework allows the creation of single dimension arrays with non-zero lower bounds they are not "vectors" and so not compatible with VB.NET typed arrays. This means they can only be seen as Array
and so cannot use normal array (index)
references.
Dim a As Array = Array.CreateInstance(GetType(Integer), {4}, {-1})
For y = LBound(a) To UBound(a)
a.SetValue(y * y, y)
Next
For y = LBound(a) To UBound(a)
Console.WriteLine($"{y}: {a.GetValue(y)}")
Next
As well as by using Option Strict Off
, you can get the (index)
syntax back by treating the array as an IList
, but then it's not an array, so you can't use LBound
and UBound
on that variable name (and you're still not avoiding boxing):
Dim nsz As IList = a
For y = LBound(a) To UBound(a)
nsz(y) = 2 - CInt(nsz(y))
Next
For y = LBound(a) To UBound(a)
Console.WriteLine($"{y}: {nsz(y)}")
Next
Multi-dimensional non-zero lower bounded arrays are compatible with VB.NET multi-dimensional typed arrays:
Dim nza(,) As Integer = DirectCast(Array.CreateInstance(GetType(Integer),
{4, 3}, {1, -1}), Integer(,))
For y = LBound(nza) To UBound(nza)
For w = LBound(nza, 2) To UBound(nza, 2)
nza(y, w) = -y * w + nza(UBound(nza) - y + LBound(nza),
UBound(nza, 2) - w + LBound(nza, 2))
Next
Next
For y = LBound(nza) To UBound(nza)
Dim ly = y
Console.WriteLine(String.Join(" ",
Enumerable.Repeat(ly & ":", 1).Concat(
Enumerable.Range(LBound(nza, 2), UBound(nza, 2) - LBound(nza, 2) + 1) _
.Select(Function(w) CStr(nza(ly, w))))))
Next
MSDN reference: Array.CreateInstance