R
Rubbrecht Philippe
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
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