开发者

Issue with passing parameters by reference into public shared methods

开发者 https://www.devze.com 2022-12-09 16:31 出处:网络
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:

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开发者_如何学C(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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消