我的列表和排序功能有些问题。我创建了类Variation和Variations。我将Variations类作为我的Variation的集合列表进行交易以将其分开。现在,当我创建集合时,我想按Variation属性位置排序,我得到错误。你能帮我吗?以下所有必要信息。
变异类:
Public Class Variation
Property ID As Integer
Property Name As String
Property Erstellungsdatum As Date
Property Position As SByte
Sub New()
End Sub
Sub New(id As Integer)
_ID = id
End Sub
Sub New(id As Integer, name As String)
_ID = id
_Name = name
End Sub
Sub New(name As String, position As Integer)
_Name = name
_Position = position
End Sub
Sub New(id As Integer, name As String, position As SByte)
_ID = id
_Name = name
_Position = position
End Sub
Sub New(id As Integer, name As String, erstellungsdatum As Date)
_ID = id
_Name = name
_Erstellungsdatum = erstellungsdatum
End Sub
End Class
班级变化:
Public Class Variations
Implements IDisposable
Implements IComparer(Of Variation)
Public MyVariationsCollection As List(Of Variation)
Sub New()
MyVariationsCollection = New List(Of Variation)
End Sub
Public Function AddToCollection(ByVal variation As Variation) As Boolean
MyVariationsCollection.Add(variation)
Return True
End Function
Public Sub RemoveFromCollection(index As Integer)
If Not IsNothing(MyVariationsCollection.Item(index)) Then
MyVariationsCollection.RemoveAt(index)
End If
End Sub
Public Function Compare(x As Variation, y As Variation) As Integer Implements IComparer(Of Variation).Compare
If x.Position > y.Position Then
Return 1
End If
If x.Position < y.Position Then
Return -1
End If
Return 0
End Function
End Class
在我正在做的代码中:
Private variations As New Variations
variations.AddToCollection(New BusinessLayer.Variation(Id, Name, StartPosition))
现在我想通过例如我在Variations中实现的位置对此列表进行排序,我希望它在那里,而不是在Variation类中,因为我希望交易变化类作为Variation对象的集合分开,如果你知道我是什么的意思。
但是,如果我这样做:
_variations.MyVariationsCollection.Sort()
然后我得到错误:
---------------------------
myprog
---------------------------
System.InvalidOperationException:
You can not compare two elements in the array. ---> System.ArgumentException: At least one object must implement IComparable element
w System.Collections.Comparer.Compare(Object a, Object b)
w System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
w System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
w System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
--- Koniec śladu stosu wyjątków wewnętrznych ---
w System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
w System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
w System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
w Cenea.UserControl1..ctor(Variations variations, Artikel mainArtikel) w C:\Users\Robert\Desktop\Cenea\Cenea\UserControl1.vb:wiersz 26
w Cenea.FrmMain.btnVariationProcees_Click(Object sender, EventArgs e) w C:\Users\Robert\Desktop\Cenea\Cenea\FrmMain.vb:wiersz 450
w System.Windows.Forms.Control.OnClick(EventArgs e)
w DevComponents.DotNetBar.ButtonX.OnClick(EventArgs e)
w DevComponents.DotNetBar.ButtonX.OnMouseUp(MouseEventArgs e)
w System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
w System.Windows.Forms.Control.WndProc(Message& m)
w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
w Cenea.ProgStart.main() w C:\Users\Robert\Desktop\Cenea\Cenea\ProgStart.vb:wiersz 14
---------------------------
OK
---------------------------
最终解决方案:
Public Class Variation
Implements IComparable(Of Variation)
...
Public Function CompareTo(pother As Variation) As Integer Implements IComparable(Of Variation).CompareTo
Return String.Compare(Me.Position, pother.Position)
End Function
...
变化:
Imports BusinessLayer
Public Class Variations
Implements IDisposable
Public collection As List(Of Variation)
Sub New()
collection = New List(Of Variation)
End Sub
Public Function AddToCollection(ByVal variation As Variation) As Boolean
collection.Add(variation)
Return True
End Function
Public Sub RemoveFromCollection(index As Integer)
If Not IsNothing(collection.Item(index)) Then
collection.RemoveAt(index)
End If
End Sub
Public Sub SortByPosition()
collection.Sort()
End Sub
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
答案 0 :(得分:0)
unlocalized error是“无法比较数组中的两个元素”。实现非通用IComparer
。
如documentation Array.Sort
中所述,使用非通用IComparer
界面:
此界面与
Array.Sort
和。{Array.BinarySearch
方法。它提供了一种自定义排序的方法 集合的顺序。有关参数的说明,请参阅比较方法 和返回值。