Arrays visualbasic中的可移植模式函数

Arrays visualbasic中的可移植模式函数,arrays,vb.net,object,analysis,Arrays,Vb.net,Object,Analysis,我试图让我的模式函数接受任何类型的数组,但没有任何进展。这是我现在得到的: Private Function Mode(ByRef list As Object) As Object Dim array() As Object = {} Try array = CType(list, Object()) Catch ex As Exception MessageBox.Show("Failed to

我试图让我的
模式
函数接受任何类型的数组,但没有任何进展。这是我现在得到的:

Private Function Mode(ByRef list As Object) As Object
        Dim array() As Object = {}
        Try
            array = CType(list, Object())
        Catch ex As Exception
            MessageBox.Show("Failed to cast array of Objects in Mode function!")
            Return Nothing
        End Try
        Dim uniqueObjects() As Integer = {array(0)}
        Dim frequency() As Integer = {1}
        For i As Integer = 0 To array.Length - 1
            For j As Integer = 0 To uniqueObjects.Length - 1 'loop through frequency
                If array(i) = uniqueObjects(j) Then
                    frequency(j) += 1
                    Exit For
                ElseIf j = uniqueObjects.Length - 1 Then
                    ReDim Preserve uniqueObjects(uniqueObjects.Length) 'add to unique objects array
                    uniqueObjects(uniqueObjects.Length - 1) = array(i)
                    ReDim Preserve frequency(frequency.Length) 'increment frequency
                    frequency(frequency.Length - 1) += 1
                End If
            Next
        Next

        Return uniqueObjects(System.Array.IndexOf(frequency, frequency.Max))
    End Function
我通常会摆脱对
CType
的缓慢调用,只向函数传递一个对象数组,但当我向函数传递一个整数数组时,会出现一个奇怪的错误:

错误1“整数的一维数组”类型的值无法转换为“对象的一维数组”,因为“整数”不是引用类型。{filename}.vb{line}{column}{project name}


这比我预想的要复杂得多。有人能提供建议吗?

让它成为一个通用函数怎么样

Private Function Mode(Of T)(ByRef array As T()) As Object
    '...
End Function

然后你会:

Dim obj As Object = Mode(Of Integer)({0, 1, 2, 3})
或:


不,我也试过了。如果数组(i)=uniqueObjects(j),则行
不起作用。它说运算符=不是为T类型的对象定义的。您需要将其更改为“If(Object.Equals(array(i),uniqueObjects(j)),然后”并且您应该将uniqueObjects声明为T的数组。
Dim obj As Object = Mode(Of Integer)({0, 1, 2, 3})
Dim obj As Integer() = Mode(Of Integer)({0, 1, 2, 3})