Issue with passing parameters by reference into pu

2019-08-24 02:46发布

I have a List(Of AddlInfo) with AddlInfo being an object.

I'm trying to pass addlInfoList by reference into a function of another class:

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

This works if I'm not passing the reference into another class, but when I try to do this, I get the following error:

Overload resolution failed because no accessible 'Sort' can be called with these arguments:

'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.

I could put the methods back into the calling class, but I'd like to be able to call these methods from different classes within my application.

I could also instantiate a fresh List in the methods, but why? Seems silly.

Any way around this? (Or do I need to explain more?)

Thanks in advance!

1条回答
叛逆
2楼-- · 2019-08-24 03:19

Try putting your compare function into a class that implements IComparer.

查看更多
登录 后发表回答