我有一个List(Of AddlInfo),其中AddlInfo是一个对象。
我正在尝试通过引用将addlInfoList传递给另一个类的函数:
Public Shared Sub SortAddlInfo(ByRef addlInfoList As List(Of AddlInfo))
addlInfoList.Sort(AddressOf Comparer)
End Sub
Private Function Comparer(ByVal x As AddlInfo, ByVal y As AddlInfo) As Integer
Dim result As Integer = x.AddlInfoType.CompareTo(y.AddlInfoType)
Return result
End Function
如果我没有将引用传递给另一个类,这是有效的,但是当我尝试这样做时,我收到以下错误:
重载解析失败,因为无法使用以下参数调用可访问的“排序”:
'Public Sub Sort(comparison As System.Comparison(Of AddlInfo))': Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
'Public Sub Sort(comparer As System.Collections.Generic.IComparer(Of AddlInfo))': 'AddressOf' expression cannot be converted to 'System.Collections.Generic.IComparer(Of MyProject.AddlInfo)' because 'System.Collections.Generic.IComparer(Of MyProject.AddlInfo)' is not a delegate type.
我可以将方法放回到调用类中,但是我希望能够在我的应用程序中从不同的类调用这些方法。
我还可以在方法中实例化一个新的List,但为什么呢?看起来很傻。
有什么方法吗? (或者我需要解释更多?)
提前致谢!
答案 0 :(得分:1)
尝试将compare函数放入实现IComparer的类中。