473,846 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Searching ArrayList of Classes

I've got arraylists of simple classes bound to controls. I need to search
through those arraylists to set the correct SelectedItem in the control.
The code looks like:

Public Class DegreeMaintenan ce
Private arrCipCodes As New ArrayList
'populate reader with data
With rdr
Do While .Read
arrCipCodes.Add (New CipCode(.GetStr ing(0), .GetString(1)))
Loop
.Close()
End With
arrCipCodes.Sor t()

Public ReadOnly Property CipCodes() As ArrayList
Get
Return arrCipCodes
End Get
End Property
End Class
'************** *************** *************** *************** ******
Public Class CipCode
Private myCipDisplay As String
Private myCipCode As String
Public Sub New(ByVal strCipDisplay As String, ByVal strCipCode As
String)
MyBase.new()
With Me
.myCipDisplay = strCipDisplay 'e.g. 01.9999 Agriculture,
Agriculture Operations and Related Sciences, Other
.myCipCode = strCipCode 'e.g. 01.9999
End With
End Sub
Public ReadOnly Property CipDisplay() As String
Get
Return myCipDisplay
End Get
End Property
Public ReadOnly Property CipCode() As String
Get
Return myCipCode
End Get
End Property
End Class
'************** *************** *************** *************** *****
And finally in a form is this code:
'
Me.cbCipCode.Se lectedIndex =
DegreeMaintenan ce.CipCodes.Bin arySearch(dd.Ci pDisplay)

When I execute this line I get this message:
Additional information: Specified IComparer threw an exception.

I'm not sure where to go from here. Do I need to implement an IComparer?
How do I search on the second element of the class members of my arraylist?

Nov 21 '05 #1
9 2879
Paul Nations wrote:
I've got arraylists of simple classes bound to controls. I need to search through those arraylists to set the correct SelectedItem in the control. The code looks like:

Public Class DegreeMaintenan ce
Private arrCipCodes As New ArrayList
'populate reader with data
With rdr
Do While .Read
arrCipCodes.Add (New CipCode(.GetStr ing(0), ..GetString(1)) ) Loop
.Close()
End With
arrCipCodes.Sor t()

Public ReadOnly Property CipCodes() As ArrayList
Get
Return arrCipCodes
End Get
End Property
End Class
'************** *************** *************** *************** ******
Public Class CipCode
Private myCipDisplay As String
Private myCipCode As String
Public Sub New(ByVal strCipDisplay As String, ByVal strCipCode As
String)
MyBase.new()
With Me
.myCipDisplay = strCipDisplay 'e.g. 01.9999 Agriculture, Agriculture Operations and Related Sciences, Other
.myCipCode = strCipCode 'e.g. 01.9999
End With
End Sub
Public ReadOnly Property CipDisplay() As String
Get
Return myCipDisplay
End Get
End Property
Public ReadOnly Property CipCode() As String
Get
Return myCipCode
End Get
End Property
End Class
'************** *************** *************** *************** *****
And finally in a form is this code:
'
Me.cbCipCode.Se lectedIndex =
DegreeMaintenan ce.CipCodes.Bin arySearch(dd.Ci pDisplay)

When I execute this line I get this message:
Additional information: Specified IComparer threw an exception.

I'm not sure where to go from here. Do I need to implement an IComparer?

Yes. BinarySearch needs to be able to compare the objects in the
ArrayList in order to search it, and it doesn't know how to compare
CipCode's.
How do I search on the second element of the class members of my

arraylist?

TWO WAYS to do this:

a) First method: Define a class that compares CipCode's and implements
IComparer; supply one of these to BinarySearch:

Public Class CipCodeComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collecti ons.IComparer.C ompare
'because we don't type check here -
'it becomes our responsibility to ensure that a CipCodeComparer
'is only ever used to compare CipCode's
Dim cx As CipCode = DirectCast(x, CipCode)
Dim cy As CipCode = DirectCast(y, CipCode)

Return String.Compare( cx.CipCode, cy.CipCode)

End Function
End Class

Hopefully this will be self-explanatory - this is a class whose sole
purpose is to provide an implementation of IComparer that compares
CipCode's by their CipCode property. Note that I have taken the
stylistic decision to explicitly passthrough to String.Compare; I could
just as easily have said

if cx.cipcode < cy.cipcode then
return -1
elseif cx.cipcode > cy.cipcode then
return 1
else
return 0
end if

I think just passing through to String.Compare better shows what is
being done.

Anyway, once you've defined this class, you just need to supply it to
BinarySearch using the appropriate overload:

Me.cbCipCode.Se lectedIndex =
DegreeMaintenan ce.CipCodes.Bin arySearch(dd.Ci pDisplay, New
CipCodeComparer )

and you should be in business.

b) Second method: have CipCode implement IComparable, so that when the
default comparer (the one that gets used when no comparer is explicitly
mentioned in the call to BinarySearch) comes calling, comparisons will
be done as you want:

add to the definition of CipCode:
Implements IComparable

Public Function CompareTo(ByVal obj As Object) As Integer
Implements System.ICompara ble.CompareTo
Dim other As CipCode = DirectCast(obj, CipCode)
Return Me.CipCode.Comp areTo(other.Cip Code)
End Function

(as before, without type checking it's our responsibility to make sure
CipCode's are only ever compared to other CipCode's)

Now when you use the comparer-less overlod of BinarySearch, it will
know get a useful answer when it asks the CipCode's to compare amongst
themselves.

btw, be sure to check you're filling the ArrayList in Code order, as
BinarySearch assumes this.
--
Larry Lard
Replies to group please

Nov 21 '05 #2
Larry;

I really appreciate the help. It's what I was trying to do before I got
desperate and posted to the newsgroup.

However, I've tried both methods you suggested and am still getting the
"Specified IComparer threw an exception." error.

I've put a break in the compare functions of both methods and the
execution never enters that function.

Reading the newsgroups, I read that I should be able to set a break in
the compare fns and see the code executing in there, but I never get
that far.

Thanks for any further consideration you can give to this.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3

OK will look again later but just quickly to check that when you
implement IComparer, you are actually passing an instance to
BinarySearch? eg

Me.cbCipCode.Se lectedIndex =
DegreeMaintenan ce.CipCodes.Bin arySearch(dd.Ci pDisplay, New MyComparer)

Just checking the obvious stuff first :)
Paul Nations wrote:
Larry;

I really appreciate the help. It's what I was trying to do before I got desperate and posted to the newsgroup.

However, I've tried both methods you suggested and am still getting the "Specified IComparer threw an exception." error.

I've put a break in the compare functions of both methods and the
execution never enters that function.

Reading the newsgroups, I read that I should be able to set a break in the compare fns and see the code executing in there, but I never get
that far.

Thanks for any further consideration you can give to this.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 21 '05 #4
Yes. Here's the line:

Me.cbCipCode.Se lectedIndex =
cDegreeMaintena nce.CipCodes.Bi narySearch(dd.C ipDisplay, New
CipCodeComparer )

Does the CipCodeComparer class need a Public Sub New or something
besides the Implements & Compare function? How does it get
instantiated?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #5
In all the examples from Dynamic Help where a comparer is built, the
comparer class is embedded within another class. Is this necessary? Can
the comparer class be a standalone? If not where would I embed the comparer
class, in the class of objects being compared or in the form calling the
comparison?

Thanks.

"Paul Nations" <pa***@adhe.ark net.edu> wrote in message
news:eU******** *****@TK2MSFTNG P15.phx.gbl...
Yes. Here's the line:

Me.cbCipCode.Se lectedIndex =
cDegreeMaintena nce.CipCodes.Bi narySearch(dd.C ipDisplay, New
CipCodeComparer )

Does the CipCodeComparer class need a Public Sub New or something
besides the Implements & Compare function? How does it get
instantiated?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #6

Sorry for not coming back Paul.

You don't need to explicitly code a constructor for your comparer class
- remember that when you don't provide a constructor, VB implicitly
creates a 'null' constructor (that takes no arguments and does
nothing), so just doing New CipCodeComparer as you have is fine.

As to where to define the comparer, the general OO principle is that
you 'hide' it as much as you can, which is why examples often have
comparers defined as private (embedded) classes. If you're going to
have a comparer that is used across classes then you would need to
define it in a broader scope.

If you're still having trouble getting it to work, try stripping the
code down to a minimal not-working example then post that.
Paul Nations wrote:
In all the examples from Dynamic Help where a comparer is built, the
comparer class is embedded within another class. Is this necessary? Can the comparer class be a standalone? If not where would I embed the comparer class, in the class of objects being compared or in the form calling the comparison?

Thanks.

"Paul Nations" <pa***@adhe.ark net.edu> wrote in message
news:eU******** *****@TK2MSFTNG P15.phx.gbl...
Yes. Here's the line:

Me.cbCipCode.Se lectedIndex =
cDegreeMaintena nce.CipCodes.Bi narySearch(dd.C ipDisplay, New
CipCodeComparer )

Does the CipCodeComparer class need a Public Sub New or something
besides the Implements & Compare function? How does it get
instantiated?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 21 '05 #7
I've stripped out everything that doesn't relate to the BinarySearch
problem I'm having. The source is in 3 files plus the data xml.

Everything is here:
http://www.arkansashighered.com/soft...rchproblem.zip
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #8

Paul Nations wrote:
I've stripped out everything that doesn't relate to the BinarySearch
problem I'm having. The source is in 3 files plus the data xml.

Everything is here:
http://www.arkansashighered.com/soft...rchproblem.zip


OK, what's happening is that you're actually searching the ArrayList of
CipCodes for a given CipCode based on the just the code, ie what you
want is:

Given the *code* and the collection, return the index of the object
with that code within the collection

whereas what BinarySearch does is

Given the *object* and the collection, return the index of that object
within that collection.
Probably the easiest way out of this is to note that when the main form
calls the edit for, at that point we *already* know the index of the
selected code within the list - so just make this one of the arguments
with which the edit form is created! - pass it through to
PopulateControl s and set the combo box's index to it.
If you do later want the ability to get a CIP object given just the
code, look into using a Hashtable.

--
Larry Lard
Replies to group please

Nov 21 '05 #9
OK. That sounds reasonable. I'll try that, since I'm already passing
in some other values in the Sub New.

Thanks for all your efforts.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #10

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

Similar topics

3
1718
by: Pablo Salazar | last post by:
Hi People Few day ago, somebody sent me this code. ArrayList al = new ArrayList(); //Insertar datos en la lista: al.Add("Ejemplo1"); al.Add("Ejemplo2"); It can store two string in ArrayList, my question is
5
7868
by: Kevin | last post by:
Hi All Can someone please tell me the difference between an Array and an ArrayList, and where I would be likely to use one over the other Thanks alot for any help
2
1565
by: Chuck Bowling | last post by:
I'm really stumped on this. Serializing an ArrayList to XML works fine when i use built in classes but when i try storing my own classes in ArrayLists and serializing them the CLR throwns an exception. Can someone give me a clue as to what's going on? using System; using System.Collections; namespace tApp {
4
2062
by: Stuart | last post by:
I have a small class of data values made up of ints and bools totalling 25 bytes. Each of these 25 byte nodes is stored in another class as an ArrayList with a few more ints. Typically there are around 400 nodes stored in each of these segment classes. - which makes each segment occupy around 10KB. I use further ArrayList to store around 30,000 of these segments, which are read in from a file. If a read in a file that contains 10,000...
10
1531
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an object with that same ID already exists in the arraylist. If it does, I would like to increase the count of the matching object inside
18
3259
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
6
1828
by: AAJ | last post by:
Hi all I have some objects that I add to an ArrayList. Each object is from the same class and has methods etc. Each can be identified by a unique property, in this case PK e.g. PK =1 for Instance1.PK PK =2 for Instance2.PK
4
3616
by: MikeY | last post by:
Hi Everyone, I'm am trying to find the best method option for searching through an ArrayList and getting various items at a time. It could be 0 to 14, 15 to 25 etc,etc. I wish .GetRange(0,45) would give more options than just zero base to whatever. Right now, (unless there is a better option) I will keep calling the ArrayList at various times getting the 15 or less items at a time and put the items into another ArrayList for my usage....
11
23667
by: KMinFL | last post by:
This is a C# VS 2008 question... Our system has 2 base classes, SingleEntity and NewPluralEntity. SingleEntity provides access to properties and methods related to manipulating data in a database table and NewPluralEntity is a generic base class that I inherit from to create strongly typed collections of single entities. Here is the declaration of our NewPluralEntity class: public abstract class NewPluralEntity<T> : ICollection<T>,...
0
9879
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7879
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7050
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5716
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5907
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4113
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.