473,396 Members | 1,886 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,396 software developers and data experts.

Overriding the Equals method to find the IndexOf an item in an ArrayList

Hi there,

According to documentation I read the ArrayList.IndexOf method uses the
Object.Equals method to loop through the items in its list and locate the
first index of an item that returns True.

Therefore, overriding the Equals method in the class definition of the items
I put in the ArrayList should make the IndexOf method use my version of the
Equals method?!

However, this does not seem to work.
Using following code in a standard VB.NET Console application returns -1 as
a result of the IndexOf method. Am I doing something wrong here?!

Best regards,
Philippe Rubbrecht

Module Main

Sub Main()

'Create ArrayList and add a few items.

Dim lst As ArrayList = New ArrayList

Dim itm As ListItem

itm = New ListItem("BE", "Belgium") : lst.Add(itm)

itm = New ListItem("DE", "Germany") : lst.Add(itm)

itm = New ListItem("NL", "Nederland") : lst.Add(itm)

'Now try to find the index of an item using the overloaded Equals method.

Dim key As String = "DE"

'Now output the index of the last item added.

System.Console.WriteLine("Index for key '{0}' = {1}", key, lst.IndexOf(key))

'Hold.

System.Console.ReadLine()

End Sub

End Module

Public Class ListItem

Private mstrKey As String

Private mstrDescription As String

Public Sub New(ByVal key As String, ByVal description As String)

Me.mstrKey = key

Me.mstrDescription = description

End Sub

Public Property Key() As String

Get

Return Me.mstrKey

End Get

Set(ByVal Value As String)

Me.mstrKey = Value

End Set

End Property

Public Property Description() As String

Get

Return Me.mstrDescription

End Get

Set(ByVal Value As String)

Me.mstrDescription = Value

End Set

End Property

Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean

If obj.GetType Is GetType(ListItem) Then

Return CType(obj, ListItem).Key.Equals(Me.Key)

ElseIf obj.GetType Is GetType(String) Then

Return CType(obj, String).Equals(Me.Key)

Else

Return False

End If

End Function

End Class
Nov 20 '05 #1
12 6691
Maybe I missed something, but I did run your code and It is returning True
( -1 ) because it found the key ( and you are returning a boolean ) as it
test as being ListItem in Type, what did you expect to be returned ?

Regards - OHM

Rubbrecht Philippe wrote:
Hi there,

According to documentation I read the ArrayList.IndexOf method uses
the Object.Equals method to loop through the items in its list and
locate the first index of an item that returns True.

Therefore, overriding the Equals method in the class definition of
the items I put in the ArrayList should make the IndexOf method use
my version of the Equals method?!

However, this does not seem to work.
Using following code in a standard VB.NET Console application returns
-1 as a result of the IndexOf method. Am I doing something wrong
here?!

Best regards,
Philippe Rubbrecht

Module Main

Sub Main()

'Create ArrayList and add a few items.

Dim lst As ArrayList = New ArrayList

Dim itm As ListItem

itm = New ListItem("BE", "Belgium") : lst.Add(itm)

itm = New ListItem("DE", "Germany") : lst.Add(itm)

itm = New ListItem("NL", "Nederland") : lst.Add(itm)

'Now try to find the index of an item using the overloaded Equals
method.

Dim key As String = "DE"

'Now output the index of the last item added.

System.Console.WriteLine("Index for key '{0}' = {1}", key,
lst.IndexOf(key))

'Hold.

System.Console.ReadLine()

End Sub

End Module

Public Class ListItem

Private mstrKey As String

Private mstrDescription As String

Public Sub New(ByVal key As String, ByVal description As String)

Me.mstrKey = key

Me.mstrDescription = description

End Sub

Public Property Key() As String

Get

Return Me.mstrKey

End Get

Set(ByVal Value As String)

Me.mstrKey = Value

End Set

End Property

Public Property Description() As String

Get

Return Me.mstrDescription

End Get

Set(ByVal Value As String)

Me.mstrDescription = Value

End Set

End Property

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean

If obj.GetType Is GetType(ListItem) Then

Return CType(obj, ListItem).Key.Equals(Me.Key)

ElseIf obj.GetType Is GetType(String) Then

Return CType(obj, String).Equals(Me.Key)

Else

Return False

End If

End Function

End Class

Nov 20 '05 #2
The IndexOf method should return the index of the first item that returns
True for the Equals method. In the sample, the IndexOf method should return
1 instead of -1 (this is the default answer when the item is not found).
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@hercules.btinternet.com...
Maybe I missed something, but I did run your code and It is returning True
( -1 ) because it found the key ( and you are returning a boolean ) as it
test as being ListItem in Type, what did you expect to be returned ?

Regards - OHM

Rubbrecht Philippe wrote:
Hi there,

According to documentation I read the ArrayList.IndexOf method uses
the Object.Equals method to loop through the items in its list and
locate the first index of an item that returns True.

Therefore, overriding the Equals method in the class definition of
the items I put in the ArrayList should make the IndexOf method use
my version of the Equals method?!

However, this does not seem to work.
Using following code in a standard VB.NET Console application returns
-1 as a result of the IndexOf method. Am I doing something wrong
here?!

Best regards,
Philippe Rubbrecht

Module Main

Sub Main()

'Create ArrayList and add a few items.

Dim lst As ArrayList = New ArrayList

Dim itm As ListItem

itm = New ListItem("BE", "Belgium") : lst.Add(itm)

itm = New ListItem("DE", "Germany") : lst.Add(itm)

itm = New ListItem("NL", "Nederland") : lst.Add(itm)

'Now try to find the index of an item using the overloaded Equals
method.

Dim key As String = "DE"

'Now output the index of the last item added.

System.Console.WriteLine("Index for key '{0}' = {1}", key,
lst.IndexOf(key))

'Hold.

System.Console.ReadLine()

End Sub

End Module

Public Class ListItem

Private mstrKey As String

Private mstrDescription As String

Public Sub New(ByVal key As String, ByVal description As String)

Me.mstrKey = key

Me.mstrDescription = description

End Sub

Public Property Key() As String

Get

Return Me.mstrKey

End Get

Set(ByVal Value As String)

Me.mstrKey = Value

End Set

End Property

Public Property Description() As String

Get

Return Me.mstrDescription

End Get

Set(ByVal Value As String)

Me.mstrDescription = Value

End Set

End Property

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean

If obj.GetType Is GetType(ListItem) Then

Return CType(obj, ListItem).Key.Equals(Me.Key)

ElseIf obj.GetType Is GetType(String) Then

Return CType(obj, String).Equals(Me.Key)

Else

Return False

End If

End Function

End Class


Nov 20 '05 #3
OK OK, I see the error now.

The index of was being passed the string ( NOT the object being searched for
Amended code below for test. I also changed the ListView class to ListView1.

OHM

'Create ArrayList and add a few items.

Dim lst As ArrayList = New ArrayList

Dim itm1, itm2, itm3 As ListItem1

itm1 = New ListItem1("BE", "Belgium") : lst.Add(itm1)

itm2 = New ListItem1("DE", "Germany") : lst.Add(itm2)

itm3 = New ListItem1("NL", "Nederland") : lst.Add(itm3)

'Now try to find the index of an item using the overloaded Equals method.

'***** NOTES THAT key now is set to an item2 - This search called the
overloaded and overridden function Equals ****

Dim key As ListItem1 = itm2

'Now output the index of the last item added.

System.Console.WriteLine("Index for key '{0}' = {1}", key, lst.IndexOf(key))

'Hold.

System.Console.ReadLine()

Rubbrecht Philippe wrote:
The IndexOf method should return the index of the first item that
returns True for the Equals method. In the sample, the IndexOf
method should return 1 instead of -1 (this is the default answer when
the item is not found).
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@hercules.btinternet.com...
Maybe I missed something, but I did run your code and It is
returning True ( -1 ) because it found the key ( and you are
returning a boolean ) as it test as being ListItem in Type, what did
you expect to be returned ?

Regards - OHM

Rubbrecht Philippe wrote:
Hi there,

According to documentation I read the ArrayList.IndexOf method uses
the Object.Equals method to loop through the items in its list and
locate the first index of an item that returns True.

Therefore, overriding the Equals method in the class definition of
the items I put in the ArrayList should make the IndexOf method use
my version of the Equals method?!

However, this does not seem to work.
Using following code in a standard VB.NET Console application
returns -1 as a result of the IndexOf method. Am I doing something
wrong here?!

Best regards,
Philippe Rubbrecht

Module Main

Sub Main()

'Create ArrayList and add a few items.

Dim lst As ArrayList = New ArrayList

Dim itm As ListItem

itm = New ListItem("BE", "Belgium") : lst.Add(itm)

itm = New ListItem("DE", "Germany") : lst.Add(itm)

itm = New ListItem("NL", "Nederland") : lst.Add(itm)

'Now try to find the index of an item using the overloaded Equals
method.

Dim key As String = "DE"

'Now output the index of the last item added.

System.Console.WriteLine("Index for key '{0}' = {1}", key,
lst.IndexOf(key))

'Hold.

System.Console.ReadLine()

End Sub

End Module

Public Class ListItem

Private mstrKey As String

Private mstrDescription As String

Public Sub New(ByVal key As String, ByVal description As String)

Me.mstrKey = key

Me.mstrDescription = description

End Sub

Public Property Key() As String

Get

Return Me.mstrKey

End Get

Set(ByVal Value As String)

Me.mstrKey = Value

End Set

End Property

Public Property Description() As String

Get

Return Me.mstrDescription

End Get

Set(ByVal Value As String)

Me.mstrDescription = Value

End Set

End Property

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean

If obj.GetType Is GetType(ListItem) Then

Return CType(obj, ListItem).Key.Equals(Me.Key)

ElseIf obj.GetType Is GetType(String) Then

Return CType(obj, String).Equals(Me.Key)

Else

Return False

End If

End Function

End Class

Nov 20 '05 #4
OK, you're right. It works because the standard Object.Equals method
verifies object references.
In both cases they are indeed equal if you pass the same object reference.

But the question was how you can override the Equals method to be able to
verify keys instead of object references!

"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@titan.btinternet.com...
OK OK, I see the error now.

The index of was being passed the string ( NOT the object being searched for Amended code below for test. I also changed the ListView class to ListView1.
OHM

'Create ArrayList and add a few items.

Dim lst As ArrayList = New ArrayList

Dim itm1, itm2, itm3 As ListItem1

itm1 = New ListItem1("BE", "Belgium") : lst.Add(itm1)

itm2 = New ListItem1("DE", "Germany") : lst.Add(itm2)

itm3 = New ListItem1("NL", "Nederland") : lst.Add(itm3)

'Now try to find the index of an item using the overloaded Equals method.

'***** NOTES THAT key now is set to an item2 - This search called the
overloaded and overridden function Equals ****

Dim key As ListItem1 = itm2

'Now output the index of the last item added.

System.Console.WriteLine("Index for key '{0}' = {1}", key, lst.IndexOf(key))
'Hold.

System.Console.ReadLine()

Rubbrecht Philippe wrote:
The IndexOf method should return the index of the first item that
returns True for the Equals method. In the sample, the IndexOf
method should return 1 instead of -1 (this is the default answer when
the item is not found).
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@hercules.btinternet.com...
Maybe I missed something, but I did run your code and It is
returning True ( -1 ) because it found the key ( and you are
returning a boolean ) as it test as being ListItem in Type, what did
you expect to be returned ?

Regards - OHM

Rubbrecht Philippe wrote:
Hi there,

According to documentation I read the ArrayList.IndexOf method uses
the Object.Equals method to loop through the items in its list and
locate the first index of an item that returns True.

Therefore, overriding the Equals method in the class definition of
the items I put in the ArrayList should make the IndexOf method use
my version of the Equals method?!

However, this does not seem to work.
Using following code in a standard VB.NET Console application
returns -1 as a result of the IndexOf method. Am I doing something
wrong here?!

Best regards,
Philippe Rubbrecht

Module Main

Sub Main()

'Create ArrayList and add a few items.

Dim lst As ArrayList = New ArrayList

Dim itm As ListItem

itm = New ListItem("BE", "Belgium") : lst.Add(itm)

itm = New ListItem("DE", "Germany") : lst.Add(itm)

itm = New ListItem("NL", "Nederland") : lst.Add(itm)

'Now try to find the index of an item using the overloaded Equals
method.

Dim key As String = "DE"

'Now output the index of the last item added.

System.Console.WriteLine("Index for key '{0}' = {1}", key,
lst.IndexOf(key))

'Hold.

System.Console.ReadLine()

End Sub

End Module

Public Class ListItem

Private mstrKey As String

Private mstrDescription As String

Public Sub New(ByVal key As String, ByVal description As String)

Me.mstrKey = key

Me.mstrDescription = description

End Sub

Public Property Key() As String

Get

Return Me.mstrKey

End Get

Set(ByVal Value As String)

Me.mstrKey = Value

End Set

End Property

Public Property Description() As String

Get

Return Me.mstrDescription

End Get

Set(ByVal Value As String)

Me.mstrDescription = Value

End Set

End Property

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean

If obj.GetType Is GetType(ListItem) Then

Return CType(obj, ListItem).Key.Equals(Me.Key)

ElseIf obj.GetType Is GetType(String) Then

Return CType(obj, String).Equals(Me.Key)

Else

Return False

End If

End Function

End Class


Nov 20 '05 #5
Yes But the basic problem is this . . .

When IndexOf is called, each item in the arrayList is compared with the
object that is submitted for searching. Equals only gets called when two
objects are of the same type. ( Makes sense really doesent it, you cant
compare a string with an integer for ex ).

If you put

debug.writeline("Overriden function has been called")

In your overriden function of Equals , it never fires if you just put a
string type in. So you can either create search object which contrains the
value member you want to search on and code your override function to test
it or simply re-think your method.

For example.

dim si as New ListItem("Im searching for this","")

indexOf(si) ' Now your Overriden function gets called.
Regards - OHM
Rubbrecht Philippe wrote:
OK, you're right. It works because the standard Object.Equals method
verifies object references.
In both cases they are indeed equal if you pass the same object
reference.

But the question was how you can override the Equals method to be
able to verify keys instead of object references!

"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@titan.btinternet.com...
OK OK, I see the error now.

The index of was being passed the string ( NOT the object being
searched for Amended code below for test. I also changed the
ListView class to ListView1.

OHM

'Create ArrayList and add a few items.

Dim lst As ArrayList = New ArrayList

Dim itm1, itm2, itm3 As ListItem1

itm1 = New ListItem1("BE", "Belgium") : lst.Add(itm1)

itm2 = New ListItem1("DE", "Germany") : lst.Add(itm2)

itm3 = New ListItem1("NL", "Nederland") : lst.Add(itm3)

'Now try to find the index of an item using the overloaded Equals
method.

'***** NOTES THAT key now is set to an item2 - This search called the
overloaded and overridden function Equals ****

Dim key As ListItem1 = itm2

'Now output the index of the last item added.

System.Console.WriteLine("Index for key '{0}' = {1}", key,
lst.IndexOf(key))

'Hold.

System.Console.ReadLine()

Rubbrecht Philippe wrote:
The IndexOf method should return the index of the first item that
returns True for the Equals method. In the sample, the IndexOf
method should return 1 instead of -1 (this is the default answer
when the item is not found).
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@hercules.btinternet.com...
Maybe I missed something, but I did run your code and It is
returning True ( -1 ) because it found the key ( and you are
returning a boolean ) as it test as being ListItem in Type, what
did you expect to be returned ?

Regards - OHM

Rubbrecht Philippe wrote:
> Hi there,
>
> According to documentation I read the ArrayList.IndexOf method
> uses the Object.Equals method to loop through the items in its
> list and locate the first index of an item that returns True.
>
> Therefore, overriding the Equals method in the class definition of
> the items I put in the ArrayList should make the IndexOf method
> use my version of the Equals method?!
>
> However, this does not seem to work.
> Using following code in a standard VB.NET Console application
> returns -1 as a result of the IndexOf method. Am I doing
> something wrong here?!
>
> Best regards,
> Philippe Rubbrecht
>
> Module Main
>
> Sub Main()
>
> 'Create ArrayList and add a few items.
>
> Dim lst As ArrayList = New ArrayList
>
> Dim itm As ListItem
>
> itm = New ListItem("BE", "Belgium") : lst.Add(itm)
>
> itm = New ListItem("DE", "Germany") : lst.Add(itm)
>
> itm = New ListItem("NL", "Nederland") : lst.Add(itm)
>
> 'Now try to find the index of an item using the overloaded Equals
> method.
>
> Dim key As String = "DE"
>
> 'Now output the index of the last item added.
>
> System.Console.WriteLine("Index for key '{0}' = {1}", key,
> lst.IndexOf(key))
>
> 'Hold.
>
> System.Console.ReadLine()
>
> End Sub
>
> End Module
>
> Public Class ListItem
>
> Private mstrKey As String
>
> Private mstrDescription As String
>
> Public Sub New(ByVal key As String, ByVal description As String)
>
> Me.mstrKey = key
>
> Me.mstrDescription = description
>
> End Sub
>
> Public Property Key() As String
>
> Get
>
> Return Me.mstrKey
>
> End Get
>
> Set(ByVal Value As String)
>
> Me.mstrKey = Value
>
> End Set
>
> End Property
>
> Public Property Description() As String
>
> Get
>
> Return Me.mstrDescription
>
> End Get
>
> Set(ByVal Value As String)
>
> Me.mstrDescription = Value
>
> End Set
>
> End Property
>
> Public Overloads Overrides Function Equals(ByVal obj As Object) As
> Boolean
>
> If obj.GetType Is GetType(ListItem) Then
>
> Return CType(obj, ListItem).Key.Equals(Me.Key)
>
> ElseIf obj.GetType Is GetType(String) Then
>
> Return CType(obj, String).Equals(Me.Key)
>
> Else
>
> Return False
>
> End If
>
> End Function
>
> End Class

Nov 20 '05 #6
On 2003-11-27, Rubbrecht Philippe <pr***************@ugine-alz.arcelor.com> wrote:
Hi there,

According to documentation I read the ArrayList.IndexOf method uses the
Object.Equals method to loop through the items in its list and locate the
first index of an item that returns True.

Therefore, overriding the Equals method in the class definition of the items
I put in the ArrayList should make the IndexOf method use my version of the
Equals method?!

However, this does not seem to work.
Using following code in a standard VB.NET Console application returns -1 as
a result of the IndexOf method. Am I doing something wrong here?!

Best regards,
Philippe Rubbrecht


<snip>

It doesn't work because arraylist is not calling your Equals function.
It calls the shared method Object.Equals - not the member Equals. In
other words, it is calling:

Object.Equalse(obj, yourobject)

You can't override that :) Actually, since your looking these up by
keys, why are you not using a hashtable? That's what they are used for
:)

Dim h As New Hashtable
Dim itm As ListItem

itm = new ListItem("BE", "Belgium") : h.Add(itm.Key, itm)
itm = new ListItem("DE", "Denmark") : h.Add(itm.Key, itm)
itm = new ListItem("NL", "Netherland") : h.Add(itm.Key, itm)

Dim key As String = "DE"

If h.Contains(key) Then
itm = h(key)
Console.WriteLine(itm.Description)
Else
Console.WriteLine("Item Not Found")
End If

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #7
Actually you can override the Equals method. I tested it See my other post.

OHM

Tom Shelton wrote:
On 2003-11-27, Rubbrecht Philippe
<pr***************@ugine-alz.arcelor.com> wrote:
Hi there,

According to documentation I read the ArrayList.IndexOf method uses
the Object.Equals method to loop through the items in its list and
locate the first index of an item that returns True.

Therefore, overriding the Equals method in the class definition of
the items I put in the ArrayList should make the IndexOf method use
my version of the Equals method?!

However, this does not seem to work.
Using following code in a standard VB.NET Console application
returns -1 as a result of the IndexOf method. Am I doing something
wrong here?!

Best regards,
Philippe Rubbrecht


<snip>

It doesn't work because arraylist is not calling your Equals function.
It calls the shared method Object.Equals - not the member Equals. In
other words, it is calling:

Object.Equalse(obj, yourobject)

You can't override that :) Actually, since your looking these up by
keys, why are you not using a hashtable? That's what they are used
for :)

Dim h As New Hashtable
Dim itm As ListItem

itm = new ListItem("BE", "Belgium") : h.Add(itm.Key, itm)
itm = new ListItem("DE", "Denmark") : h.Add(itm.Key, itm)
itm = new ListItem("NL", "Netherland") : h.Add(itm.Key, itm)

Dim key As String = "DE"

If h.Contains(key) Then
itm = h(key)
Console.WriteLine(itm.Description)
Else
Console.WriteLine("Item Not Found")
End If

HTH

Nov 20 '05 #8
On 2003-11-27, One Handed Man <Bo****@Duck.net> wrote:
Actually you can override the Equals method. I tested it See my other post.

OHM


Yes, you can - the instance method. The problem is that
ArrayList.IndexOf isn't calling the instance method. Put a
break point in your Equals - it never gets called. That's
because the ArrayList.IndexOf is calling the class method
Equals. That you can't override, because it is a member of
the class, not your instance... Am I clear?

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #9
I'm referring to the Equals methods of the instances of the Objects in the
List. I did put a breakpoint in the overriden functions and they do get
called provided that the object being passed to IndexOf has other objects of
the same class in the List of objects contained in the ArrayList, this makes
perfect sense to me.

.. . . .Am I Clear ?

OHM
Tom Shelton wrote:
On 2003-11-27, One Handed Man <Bo****@Duck.net> wrote:
Actually you can override the Equals method. I tested it See my
other post.

OHM


Yes, you can - the instance method. The problem is that
ArrayList.IndexOf isn't calling the instance method. Put a
break point in your Equals - it never gets called. That's
because the ArrayList.IndexOf is calling the class method
Equals. That you can't override, because it is a member of
the class, not your instance... Am I clear?

Nov 20 '05 #10
On 2003-11-28, One Handed Man <Bo****@Duck.net> wrote:
I'm referring to the Equals methods of the instances of the Objects in the
List. I did put a breakpoint in the overriden functions and they do get
called provided that the object being passed to IndexOf has other objects of
the same class in the List of objects contained in the ArrayList, this makes
perfect sense to me.

. . . .Am I Clear ?

OHM


On further investigation - you do indeed appear to be correct. So, I
apologize if I have offended you - though that was never my intention.

If you call it using a string, then the overridden equals is never
called (which is what I was trying), but if you pass in a ListItem,
then it is. And you can then do a value comparison. Which means
that the ArrayList.IndexOf is only calling the instance equals if the
types match....

So, this doesn't work...

Dim key As String = "DE"
Console.WriteLine ("Index for key '{0}' = {1}", key, lst.IndexOf(key))

But, this does:

Dim key As New ListItem("DE", String.Empty)
Console.WriteLine( _
"Index for key '{0}' = {1}", key.Key, lst.IndexOf(key))

Still, I believe the OP would be better off using a hashtable, since
that is really what it looks like he is trying to accomplish anyway.
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #11
Hi Tom,
No offence taken, we are in agreement. At the end of the day,
facts are all thats count.

Best Regards - OHM

Tom Shelton wrote:
On 2003-11-28, One Handed Man <Bo****@Duck.net> wrote:
I'm referring to the Equals methods of the instances of the Objects
in the List. I did put a breakpoint in the overriden functions and
they do get called provided that the object being passed to IndexOf
has other objects of the same class in the List of objects contained
in the ArrayList, this makes perfect sense to me.

. . . .Am I Clear ?

OHM


On further investigation - you do indeed appear to be correct. So, I
apologize if I have offended you - though that was never my intention.

If you call it using a string, then the overridden equals is never
called (which is what I was trying), but if you pass in a ListItem,
then it is. And you can then do a value comparison. Which means
that the ArrayList.IndexOf is only calling the instance equals if the
types match....

So, this doesn't work...

Dim key As String = "DE"
Console.WriteLine ("Index for key '{0}' = {1}", key, lst.IndexOf(key))

But, this does:

Dim key As New ListItem("DE", String.Empty)
Console.WriteLine( _
"Index for key '{0}' = {1}", key.Key, lst.IndexOf(key))

Still, I believe the OP would be better off using a hashtable, since
that is really what it looks like he is trying to accomplish anyway.

Nov 20 '05 #12
I know, in the sample I gave I would be better of using a hashtable.
But I just posted this as a sample of what I was trying to do.... overriding
the Equals method to find items in an arraylist.

I know now I have to supply an object of the same type to have the IndexOf
method use my overridden version of the Equals method.
Although I believe that this is not a necessary requirement if you try to
find an item in the Items list of a ComboBox for example? I have to check
this myself to be 100% sure about this, though!

Thanks for the info!
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@sparta.btinternet.com...
Hi Tom,
No offence taken, we are in agreement. At the end of the day, facts are all thats count.

Best Regards - OHM

Tom Shelton wrote:
On 2003-11-28, One Handed Man <Bo****@Duck.net> wrote:
I'm referring to the Equals methods of the instances of the Objects
in the List. I did put a breakpoint in the overriden functions and
they do get called provided that the object being passed to IndexOf
has other objects of the same class in the List of objects contained
in the ArrayList, this makes perfect sense to me.

. . . .Am I Clear ?

OHM


On further investigation - you do indeed appear to be correct. So, I
apologize if I have offended you - though that was never my intention.

If you call it using a string, then the overridden equals is never
called (which is what I was trying), but if you pass in a ListItem,
then it is. And you can then do a value comparison. Which means
that the ArrayList.IndexOf is only calling the instance equals if the
types match....

So, this doesn't work...

Dim key As String = "DE"
Console.WriteLine ("Index for key '{0}' = {1}", key, lst.IndexOf(key))

But, this does:

Dim key As New ListItem("DE", String.Empty)
Console.WriteLine( _
"Index for key '{0}' = {1}", key.Key, lst.IndexOf(key))

Still, I believe the OP would be better off using a hashtable, since
that is really what it looks like he is trying to accomplish anyway.


Nov 20 '05 #13

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

Similar topics

3
by: Frank Wisniewski | last post by:
Is this suppose to work like this: I have a class called foo in which I tried to override equals and return different answers based on what was passed in. public class foo { private string...
3
by: pagates | last post by:
Hello Gurus, I have a listview, and I only want to add unique items. The problem is, my code is blowing by my "Contains" statement, adding the item, and then hitting my Compare code in the...
1
by: Ryan McLean | last post by:
Hi everyone! What is happening is the method: sub_btnSubmitClicked is being executed every time any other object with a Handler is executed. I am trying not to use the withevents and handles...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
8
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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,...

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.