473,432 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

how to do Generic.List.BinarySearch on byRef data?

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

Jun 27 '08 #1
0 1235

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Rockwell | last post by:
I am using a generics List<MyStructure> object of a structure of information I need in my application. Initially I created a predicate to retrieve the correct struct of data, but I am finding that...
5
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the...
6
by: Urs Eichmann | last post by:
While experimenting with the Feb CTP Edition of VB 2005, I came across "generic procedures". You can write: Public Class Foo Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)...
7
by: Sehboo | last post by:
We have several generic List objects in our project. Some of them have about 1000 items in them. Everytime we have to find something, we have to do a for loop. There is one method which does the...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
9
by: Rick | last post by:
I have a large list of objects where each object has a unique (non-overlapping) date range. The list is sorted chronologically. What is the most efficient way to search this list for a single...
6
by: zfareed | last post by:
<code> #include <iostream> #include <fstream> const int MAX_LENGTH = 10; using namespace std;
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
2
by: Andrew Jocelyn | last post by:
Hi I need to do a case insensitive search on a Generic list. I'm trying to use the BinarySearch method but I get a complier error "cannot convert from...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.