views:

59

answers:

1

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  A: 

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

Sonny Boy
Matthew: Thanks for responding. Tried what you suggested but I think I'm getting in further over my head. ;) Can you tell me what your suggestion would accomplish?
John at CashCommons
I think my question was ill-posed so I'll accept your answer and move on.
John at CashCommons
John,What you'll want to do it create a new class and have it implement IComparer. The IDE will then create the Compare function for you and all you need to do it put in the code for the comparison.After that, create an instance of the class and pass that into the Sort method for your addInfoList object.Check out this URL for more info:http://www.devcity.net/Articles/20/1/20020304.aspxPay special attention to the CMySort class used in the example.
Sonny Boy