A
AMercer
I am able to use Array.Sort with an IComparer (generic or otherwise). I make
a class that implements IComparer and do the sorting in that class with an
overload like:
Array.Sort(Of SomeClass)(MyArray, Start, Length, Me).
So far, so good. What I need to do is have multiple IComparers so I can
sort the array in different ways under program control. If I try to do this
in my sorting class, I can't use an Implements clause (only one comparer
allowed), but it seems that I can get around that with the following
arrangement that is stripped down to its simplest form:
Public Class SomeClass
Public Function Compare1(ByVal x As Integer, ByVal y As Integer) As Integer
Return x - y
End Function
Public Function Compare2(ByVal x As Integer, ByVal y As Integer) As Integer
Return y - x
End Function
Public Sub SomeSub()
Dim a() As Integer = {6, 2, 4, 3, 5, 1}
Array.Sort(Of Integer)(a, AddressOf Compare1)
Array.Sort(Of Integer)(a, AddressOf Compare2)
'Array.Sort(Of Integer)(a, 0, 6, AddressOf Compare1)
'AddressOf' expression cannot be converted to
'System.Collections.Generic.IComparer(Of Integer)' because
'System.Collections.Generic.IComparer(Of Integer)' is not a delegate type.
End Sub
End Class
The class contains two comparers without an Implements statement. The first
two sort statements compile ok and run ok. The third (commented out) sort
statement diagnoses with the 'not a delegate type' error.
This looks like a bug in the VB compiler to me. How can one overload work
and the other fail? Regardless, does anyone know how to do this kind of
thing? I don't want to make a separate class for each comparer because all
the comparers need to get at come class variables. That means that in the
SomeClass example above, both comparers need visibility to SomeClass
properties. I guess I could make one class per comparer and have each
inherit SomeClass, but this seems convoluted. What is more natural is to
have multiple Compare functions in one class. Any ideas?
a class that implements IComparer and do the sorting in that class with an
overload like:
Array.Sort(Of SomeClass)(MyArray, Start, Length, Me).
So far, so good. What I need to do is have multiple IComparers so I can
sort the array in different ways under program control. If I try to do this
in my sorting class, I can't use an Implements clause (only one comparer
allowed), but it seems that I can get around that with the following
arrangement that is stripped down to its simplest form:
Public Class SomeClass
Public Function Compare1(ByVal x As Integer, ByVal y As Integer) As Integer
Return x - y
End Function
Public Function Compare2(ByVal x As Integer, ByVal y As Integer) As Integer
Return y - x
End Function
Public Sub SomeSub()
Dim a() As Integer = {6, 2, 4, 3, 5, 1}
Array.Sort(Of Integer)(a, AddressOf Compare1)
Array.Sort(Of Integer)(a, AddressOf Compare2)
'Array.Sort(Of Integer)(a, 0, 6, AddressOf Compare1)
'AddressOf' expression cannot be converted to
'System.Collections.Generic.IComparer(Of Integer)' because
'System.Collections.Generic.IComparer(Of Integer)' is not a delegate type.
End Sub
End Class
The class contains two comparers without an Implements statement. The first
two sort statements compile ok and run ok. The third (commented out) sort
statement diagnoses with the 'not a delegate type' error.
This looks like a bug in the VB compiler to me. How can one overload work
and the other fail? Regardless, does anyone know how to do this kind of
thing? I don't want to make a separate class for each comparer because all
the comparers need to get at come class variables. That means that in the
SomeClass example above, both comparers need visibility to SomeClass
properties. I guess I could make one class per comparer and have each
inherit SomeClass, but this seems convoluted. What is more natural is to
have multiple Compare functions in one class. Any ideas?