ConvertAll fails with NullReferenceException
I am trying to flatten a multi-dimensional array with ConvertAll but I can't get it to work:
Private Function Flatten(ByRef a As Object) As Object
Dim elements As Integer = 0
Dim size As Integer
For r = 0 To a.Rank - 1
size = a.GetUpperBound(r) - a.GetLowerBound(r) + 1
If elements = 0 Then
elements = size
Else
elements = elements * size
End If
Next
Dim result(elements - 1) As Object
' Fails !
result = a.ConvertAll(a, New Converter(Of Object, Object)(Function(x) x))
Return result
End Function
Sub Main()
Dim a(,) As Integer = {{1, 2, 3}, {4, 5, 6}}
Dim b(5) As Integer
b = Flatten(a)
End Sub
Why does it throw NullReference开发者_运维技巧Exception?
Thanks
According to the msdn documentation, Array.ConvertAll()
takes zero based one-dimensional array for TInput
. You have a(,)
defined as 2 dimensional
精彩评论