B
Buky
So, Let's say that I have an class with properies like Name, LastName & ID.
I put them into List(of myClass) and I would like to search through that
List based on my search criteria (Name & Id for example).
So, I create an Comparer, and provide it's 'reference' to an BinarySearch
functio, but each time I got incorrect results.
Where could be problem in following code?
Public Class myClass
Public Id As Int32
Public Name As String
Public LastName As String
Public Sub New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
Public Sub test_novi()
Dim Lista As New List(Of myClass)
Dim y As Int32
Dim C As IComparer(Of MyClass)
C = New MyCompare
Dim T As New MyClass
T.Id = 1
T.Name = 2
Lista.Add(T)
T = New MyClass
T.Id = 2
T.Name = 4
Lista.Add(T)
T = New MyClass
T.Id = 3
T.Name = 6
Lista.Add(T)
Lista.Sort()
Dim T1 As New MyClass
T1.Id = 1
T1.Name = 2
y = Lista.BinarySearch(T1, C)
End Sub
End Module
Public Class MyCompare
Implements IComparer(Of MyClass)
Public Sub New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Public Function Compare(ByVal x As MyClass,byVal y As MyClass) As
Integer Implements IComparer(Of MyClass).Compare
If x.Id = y.Id Then
If x.Name = y.Name Then
Return 0
Else
Return -1
End If
Else
Return -2
End If
End Function
End Class
I put them into List(of myClass) and I would like to search through that
List based on my search criteria (Name & Id for example).
So, I create an Comparer, and provide it's 'reference' to an BinarySearch
functio, but each time I got incorrect results.
Where could be problem in following code?
Public Class myClass
Public Id As Int32
Public Name As String
Public LastName As String
Public Sub New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
Public Sub test_novi()
Dim Lista As New List(Of myClass)
Dim y As Int32
Dim C As IComparer(Of MyClass)
C = New MyCompare
Dim T As New MyClass
T.Id = 1
T.Name = 2
Lista.Add(T)
T = New MyClass
T.Id = 2
T.Name = 4
Lista.Add(T)
T = New MyClass
T.Id = 3
T.Name = 6
Lista.Add(T)
Lista.Sort()
Dim T1 As New MyClass
T1.Id = 1
T1.Name = 2
y = Lista.BinarySearch(T1, C)
End Sub
End Module
Public Class MyCompare
Implements IComparer(Of MyClass)
Public Sub New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Public Function Compare(ByVal x As MyClass,byVal y As MyClass) As
Integer Implements IComparer(Of MyClass).Compare
If x.Id = y.Id Then
If x.Name = y.Name Then
Return 0
Else
Return -1
End If
Else
Return -2
End If
End Function
End Class