CType(Array.CreateInstance(...), Integer()) InvalidCastException

wfunction

Well-known member
Joined
Oct 31, 2006
Messages
46
Programming Experience
1-3
I get an InvalidCastException with the following code:
VB.NET:
Dim arr() As Integer = DirectCast(System.Array.CreateInstance(GetType(Integer), New Integer() {10}, New Integer() {1}), Integer())
As well as
VB.NET:
Dim arr() As Integer = CType(System.Array.CreateInstance(GetType(Integer), New Integer() {10}, New Integer() {1}), Integer())

Although I this works perfectly fine:
VB.NET:
Dim arr(,) As Integer = DirectCast(System.Array.CreateInstance(GetType(Integer), New Integer() {10, 10}, New Integer() {1, 1}), Integer(,))

I'm aware that one is 1-D and the other is 2-D, but how can I create a one-dimensional array of Integer with the lower bound 1?
It seems impossible!!

FYI: This is VB.NET with .NET Framework 2.0.
 
Last edited:
how can I create a one-dimensional array of Integer with the lower bound 1?
It seems impossible!!
Using the System.Array class, casting it to an one-dimensional array (that must zero-base) is impossible.
VB.NET:
Dim bls As System.Array = Array.CreateInstance(GetType(Boolean), New Integer() {2}, New Integer() {1})
 
But I don't get it:
Why is 2-D casting possible, but not 1-D?
Using System.Array does not help because of type checking.
 
Back
Top