ConvertAll失败,出现NullReferenceException

时间:2011-07-21 10:44:13

标签: .net vb.net arrays

我试图用ConvertAll压平一个多维数组,但我无法让它工作:

 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

为什么会抛出NullReferenceException?

由于

1 个答案:

答案 0 :(得分:1)

根据msdn documentationArray.ConvertAll()TInput采用零基础的一维数组。您将a(,)定义为2维