System.ArgumentException: Mindestens ein Objekt muss IComparable implementieren

stimmen
0

In meiner MergeCollectionKlasse habe ich overide InsertItemMethode für konkreten Fall zu prüfen. Dennoch , wenn es um die Linie kommt Items.Any(..wirft es mir , wie unten Ausnahme.

System.ArgumentException: 'At least one object must implement IComparable.'

Merge-Klasse:

Public Class Merge
        Property Min As Integer
        Property Max As Integer?
        Property Value As Double

        Public Sub New(min As Integer, max As Integer?, value As Integer)
            Me.Min = min
            Me.Max = max
            Me.Value = value
        End Sub
End Class

Public Enum SortCriteria
        MinThenMax
        MaxThenMin
End Enum

Einige Vergleich:

Public Class MergeComparer
        Implements IComparer(Of Merge)  

        Public SortBy As SortCriteria = SortCriteria.MinThenMax

        Public Function Compare(x As Merge, y As Merge) As Integer Implements IComparer(Of Merge).Compare
           'to be implemented
        End Function
End Class

Collection-Klasse:

Public Class MergeCollection
          Inherits Collection(Of Merge)

        Public SortBy As SortCriteria = SortCriteria.MinThenMax

        Protected Overrides Sub InsertItem(index As Integer, item As Merge)
            if IsNothing(item.Max)
                If Items.Any(Function(myObject) IsNothing(Items.Max)) Then
                    Return
                End If
            End If
            MyBase.InsertItem(index, item)
        End Sub   

        Public Sub Sort()
            Dim allItems = Items.ToArray()

            Array.Sort(allItems)

            For i = 0 To allItems.GetUpperBound(0)
                Items(i) = allItems(i)
            Next
        End Sub

        Public Sub Sort(comparison As Comparison(Of Merge))
            Dim allItems = Items.ToArray()

            Array.Sort(allItems, comparison)

            For i = 0 To allItems.GetUpperBound(0)
                Items(i) = allItems(i)
            Next
        End Sub

        Public Sub Sort(comparer As IComparer(Of Merge))
            Dim allItems = Items.ToArray()

            Array.Sort(allItems, comparer)

            For i = 0 To allItems.GetUpperBound(0)
                Items(i) = allItems(i)
            Next
        End Sub

End Class
Veröffentlicht am 18/12/2018 um 11:06
quelle vom benutzer
In anderen Sprachen...                            


1 antworten

stimmen
0

Der Code verlangt Items.Max. Um den Maximalwert zu finden, hat es die Einzelteile miteinander zu vergleichen.

Public Class Merge
    Implements IComparable(Of Merge)

    Property Min As Integer
    Property Max As Integer?
    Property Value As Double

    Public Sub New()
        ' empty constructor
    End Sub

    Public Sub New(min As Integer, max As Integer?, value As Integer)
        Me.Min = min
        Me.Max = max
        Me.Value = value
    End Sub

    Public Overloads Function CompareTo(other As Merge) As Integer Implements IComparable(Of Merge).CompareTo

        If other Is Nothing Then Return 1

        ' Could use
        ' Return (Me.Value).CompareTo(other.Value)
        ' instead of the following code...
        If Me.Value > other.Value Then Return 1
        If Me.Value = other.Value Then Return 0

        Return -1

    End Function

End Class

Ich denke, dass Sie ein anderes Kriterium für den Vergleich werden soll.

Beantwortet am 18/12/2018 um 16:15
quelle vom benutzer

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more