Arraylist.Index(Object) help please. [modified]
-
I am trying to get the index of an object in an Arraylist. Please see my example. When run I get -1 for the values of Indexof. I am sure this is a rookie error but any help would be appreciated. Cheers Public Class Plateobj Public Name As String Public Order As Integer End Class Private Sub Go() Dim Plate As New Plateobj Dim List As New ArrayList 'Populate arraylist Plate.Name = "Geoff" Plate.Order = 3 List.Add(Plate) Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 2 List.Add(Plate) Plate.Name = "Paul" Plate.Order = 1 List.Add(Plate) 'Locate the index of each of the following MessageBox.Show(List.IndexOf(Plate), "Paul") Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 1 MessageBox.Show(List.IndexOf(Plate), "Dan") Plate = New Plateobj Plate.Name = "Geoff" Plate.Order = 3 MessageBox.Show(List.IndexOf(Plate), "Geoff") End Sub
modified on Friday, November 27, 2009 6:36 AM
-
I am trying to get the index of an object in an Arraylist. Please see my example. When run I get -1 for the values of Indexof. I am sure this is a rookie error but any help would be appreciated. Cheers Public Class Plateobj Public Name As String Public Order As Integer End Class Private Sub Go() Dim Plate As New Plateobj Dim List As New ArrayList 'Populate arraylist Plate.Name = "Geoff" Plate.Order = 3 List.Add(Plate) Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 2 List.Add(Plate) Plate.Name = "Paul" Plate.Order = 1 List.Add(Plate) 'Locate the index of each of the following MessageBox.Show(List.IndexOf(Plate), "Paul") Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 1 MessageBox.Show(List.IndexOf(Plate), "Dan") Plate = New Plateobj Plate.Name = "Geoff" Plate.Order = 3 MessageBox.Show(List.IndexOf(Plate), "Geoff") End Sub
modified on Friday, November 27, 2009 6:36 AM
Hi, You create 5 instances of Plateobj and add 3 of them to the list. when you reach the first MessageBox.Show statetement, the list contains: Geoff, Dan, Paul. Nothing changes afterwards, as you don't add the later Plateobj objects. So the output should be: 2, -1, -1 BTW: I suggest you improve your naming conventions; normally variables use lower-case (plate), and class names don't include "class" or "obj" or such. FWIW: the list order is independent of the Order member inside Plateobj (until you come up with some code to sort the list based on the Order member of course). :)
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
I am trying to get the index of an object in an Arraylist. Please see my example. When run I get -1 for the values of Indexof. I am sure this is a rookie error but any help would be appreciated. Cheers Public Class Plateobj Public Name As String Public Order As Integer End Class Private Sub Go() Dim Plate As New Plateobj Dim List As New ArrayList 'Populate arraylist Plate.Name = "Geoff" Plate.Order = 3 List.Add(Plate) Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 2 List.Add(Plate) Plate.Name = "Paul" Plate.Order = 1 List.Add(Plate) 'Locate the index of each of the following MessageBox.Show(List.IndexOf(Plate), "Paul") Plate = New Plateobj Plate.Name = "Dan" Plate.Order = 1 MessageBox.Show(List.IndexOf(Plate), "Dan") Plate = New Plateobj Plate.Name = "Geoff" Plate.Order = 3 MessageBox.Show(List.IndexOf(Plate), "Geoff") End Sub
modified on Friday, November 27, 2009 6:36 AM
You are generating new plate objects for comparison, however IndexOf does an object comparison. The newly created objects will never match anything in your list. What
Indexof
is doing is iterating through the list callingEquals
to compare each object in turn. The only way your comparison can work is to override theEquals
method on yourPlateObj
. E.g.Public Overrides Function Equals(ByVal obj As Object) As Boolean
Dim plate As Plateobj = TryCast(obj, Plateobj)' Return False if the obj is not a plate
If plate Is Nothing Then Return False' Must have same name
If plate.Name <> Me.Name Then Return False' Must have same order
If plate.Order <> Me.Order Then Return False' We have a match
Return True
End FunctionIf only the names need to match then only compare the names.
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
-
You are generating new plate objects for comparison, however IndexOf does an object comparison. The newly created objects will never match anything in your list. What
Indexof
is doing is iterating through the list callingEquals
to compare each object in turn. The only way your comparison can work is to override theEquals
method on yourPlateObj
. E.g.Public Overrides Function Equals(ByVal obj As Object) As Boolean
Dim plate As Plateobj = TryCast(obj, Plateobj)' Return False if the obj is not a plate
If plate Is Nothing Then Return False' Must have same name
If plate.Name <> Me.Name Then Return False' Must have same order
If plate.Order <> Me.Order Then Return False' We have a match
Return True
End FunctionIf only the names need to match then only compare the names.
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
Mr Kuryakin Thanks for the answer this makes sense I did try before posting a compareto function in the plateobj class, which I have successfully used for sorting. Ok so how to use the code you posted. 1) I assume I add this to the plateobj class ? 2) I get an error that suggests instead of using Overides I should use Overloads 3) Dim plate As Plateobj = TryCast(obj, Plateobj)gives two errors - TryCast not declared - 'Plateobj' is a type and cannot be used as an expression. Could I ask you to insert your code into my example and post back. Many Thanks Geoff