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 class where only the
"searched" property has a value. I expected to get the index into the
arraylist where I could then get the entire class instance. However, the
'indexof' is never calling my overloaded, overrides Equals method. Here is
the code snippet:
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim PeopleCollectio n As New ArrayList
Dim Customer As New Person
Customer.Age = 25
Customer.IsMale = "Yes"
PeopleCollectio n.Add(Customer)
Dim y As New Person
y.Age = 25
Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this always
returns -1, meaning it didn't match anything
End Sub
Public Class Person
Private _IsMale As String
Private _age As Integer
Public Overloads Overrides Function equals(ByVal obj As Object) As Boolean
If obj Is Nothing Then Return False
If Me.GetType Is obj.GetType Then
Return Me.Age = CType(obj, Person).Age
End If
End Function
Public Property IsMale() As String
Get
Return _IsMale
End Get
Set(ByVal Value As String)
_IsMale = Value
End Set
End Property
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal Value As Integer)
_age = Value
End Set
End Property
End Class
In this example I wanted to get the instance of the PERSON whose age is 25.
If I put a breakpoint in my overloads overrides equals method, it never
breaks. My EQUALS method is not being called.
Can anybody explain what is going on, and how I can get my indexof to do
what I need it to do?
Thanks, John 18 4741
JohnR wrote: 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 class where only the
"searched" property has a value. I expected to get the index into
the arraylist where I could then get the entire class instance.
However, the 'indexof' is never calling my overloaded, overrides Equals method.
Here is the code snippet:
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim PeopleCollectio n As New ArrayList
Dim Customer As New Person Customer.Age = 25 Customer.IsMale = "Yes"
PeopleCollectio n.Add(Customer)
Dim y As New Person y.Age = 25 Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this always returns -1, meaning it didn't match anything End Sub
Public Class Person Private _IsMale As String Private _age As Integer Public Overloads Overrides Function equals(ByVal obj As Object) As
Boolean If obj Is Nothing Then Return False If Me.GetType Is obj.GetType Then Return Me.Age = CType(obj, Person).Age End If End Function
[snip] In this example I wanted to get the instance of the PERSON whose age
is 25. If I put a breakpoint in my overloads overrides equals method, it
never breaks. My EQUALS method is not being called.
Can anybody explain what is going on, and how I can get my indexof to
do what I need it to do?
This is technically a feature not a bug (doesn't help I know), as the
behaviour is as documented. In the docs for ArrayList.Index Of, you will
see it says:
Remarks
....
This method determines equality by calling Object.Equals.
which is why your class's Equals method is not being called. To get the
behaviour you want you have a number of choices which include these as
I think the most obvious two:
- Implement a strongly-typed ArrayList for storing solely Person
objects, that uses Person.Equals when doing an IndexOf operation;
- Use a different collection class, one that explicitly supports the
use of overriden Equals operators. For example, a Hashtable uses the
Equals implementation of the key objects, rather than always using
Object.Equals
If you need wither or both of these options explained more, feel free
to ask :)
--
Larry Lard
Replies to group please
Hi Larry, - Implement a strongly-typed ArrayList for storing solely
Person objects, that uses Person.Equals when doing an IndexOf
operation;
I'm not exactly sure what you mean...
Every entry into the arraylist was of type PERSON. But, still, the indexof
didn't call the object.equals in the Person class. I'm guessing that it is
calling a object.equals method that is in the ArrayList class, so I tried
to create a new class ARRAYLISTPERSON which inherits from arraylist. I then
put the overrides overloads function equals(byval obj as object) as boolean
in the arraylistperson class def'n and stored my PERSON instances in this
class rather than the regular arraylist class. Unfortunately, when the
indexof method was called, my Equals function was not called, so I'm still
at a loss.
Do you have a small code example of how to override the Object.equals
method of an arraylist.index of?
Thanks, John
"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. . JohnR wrote: 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 class where only the
"searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the code snippet:
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim PeopleCollectio n As New ArrayList
Dim Customer As New Person Customer.Age = 25 Customer.IsMale = "Yes"
PeopleCollectio n.Add(Customer)
Dim y As New Person y.Age = 25 Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this always returns -1, meaning it didn't match anything End Sub
Public Class Person Private _IsMale As String Private _age As Integer Public Overloads Overrides Function equals(ByVal obj As Object) As Boolean If obj Is Nothing Then Return False If Me.GetType Is obj.GetType Then Return Me.Age = CType(obj, Person).Age End If End Function [snip] In this example I wanted to get the instance of the PERSON whose age
is 25. If I put a breakpoint in my overloads overrides equals method, it never breaks. My EQUALS method is not being called.
Can anybody explain what is going on, and how I can get my indexof to do what I need it to do?
This is technically a feature not a bug (doesn't help I know), as the behaviour is as documented. In the docs for ArrayList.Index Of, you will see it says:
Remarks ... This method determines equality by calling Object.Equals.
which is why your class's Equals method is not being called. To get the behaviour you want you have a number of choices which include these as I think the most obvious two:
- Implement a strongly-typed ArrayList for storing solely Person objects, that uses Person.Equals when doing an IndexOf operation;
- Use a different collection class, one that explicitly supports the use of overriden Equals operators. For example, a Hashtable uses the Equals implementation of the key objects, rather than always using Object.Equals
If you need wither or both of these options explained more, feel free to ask :)
-- Larry Lard Replies to group please
JohnR wrote: Hi Larry,
> - Implement a strongly-typed ArrayList for storing
solely Person > objects, that uses Person.Equals when doing an
IndexOf operation;
I'm not exactly sure what you mean...
Every entry into the arraylist was of type PERSON. But, still, the
indexof didn't call the object.equals in the Person class. I'm guessing that
it is calling a object.equals method that is in the ArrayList class,
An ArrayList will *always* use Object.Equals (the Equals method defined
in the base class Object) when checking for equality in its IndexOf
method. That is just the way ArrayList is implemented.
so I tried to create a new class ARRAYLISTPERSON which inherits from arraylist.
I then put the overrides overloads function equals(byval obj as object) as
boolean in the arraylistperson class def'n and stored my PERSON instances in
this class rather than the regular arraylist class. Unfortunately, when
the indexof method was called, my Equals function was not called, so I'm
still at a loss.
Supplying a new Equals method, wherever you do it, won't change
ArrayList's behaviour - it will always use Object.Equals. Do you have a small code example of how to override the
Object.equals method of an arraylist.index of?
What you need to change the behaviour of is the *IndexOf* method. Here
is the general idea:
Public Class ArrayListOfPers on
Inherits ArrayList
Public Shadows Function IndexOf(ByVal p As Person) As Integer
Dim iterp As Person
Dim i As Integer
For i = 0 To Me.Count - 1
iterp = DirectCast(Me(i ), Person)
If p.Equals(iterp) Then Return i
Next
Return -1
End Function
End Class
Usage example:
Dim al As ArrayList = New ArrayList
al.Add(New Person(10))
al.Add(New Person(5))
MsgBox(al.Index Of(New Person(5)))
'this will show -1, because ArrayList uses Object.Equals to
compare
'while performing the IndexOf method
'and Object.Equals requires reference equivalence
Dim alp As ArrayListOfPers on = New ArrayListOfPers on
alp.Add(New Person(10))
alp.Add(New Person(5))
MsgBox(alp.Inde xOf(New Person(5)))
'this will return 1, because ArrayListOfPers on uses
Person.Equals
'while performing IndexOf(As Person)
**** PLEASE NOTE **** that this lacks robustness in a number of ways,
and this is *not* how I would suggest implementing a strongly-typed
collection in anything larger than a toy (ie personal-use only)
application. The main problems are that: There is nothing to stop a
careless client of ArrayListOfPers on adding a non-Person; the Item()
property is still returned As Object so must be casted to Person; an
ArrayListOfPers on can be casted down to ArrayList and lose all its
custom behaviour. To reiterate, DON'T do custom collections in this way
in seroius apps! This is just an example! You can find many posts on
the correct ways to implement custom collections here in this group. Or
start a new thread and ask, as that is a separate topic :) Thanks, John
"Larry Lard" <la*******@hotm ail.com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.com.. . JohnR wrote: 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 class where only
the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the code snippet:
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim PeopleCollectio n As New ArrayList
Dim Customer As New Person Customer.Age = 25 Customer.IsMale = "Yes"
PeopleCollectio n.Add(Customer)
Dim y As New Person y.Age = 25 Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this
always returns -1, meaning it didn't match anything End Sub
Public Class Person Private _IsMale As String Private _age As Integer Public Overloads Overrides Function equals(ByVal obj As Object) As Boolean If obj Is Nothing Then Return False If Me.GetType Is obj.GetType Then Return Me.Age = CType(obj, Person).Age End If End Function [snip] In this example I wanted to get the instance of the PERSON whose
age is 25. If I put a breakpoint in my overloads overrides equals method, it never breaks. My EQUALS method is not being called.
Can anybody explain what is going on, and how I can get my indexof
to do what I need it to do?
This is technically a feature not a bug (doesn't help I know), as
the behaviour is as documented. In the docs for ArrayList.Index Of, you
will see it says:
Remarks ... This method determines equality by calling Object.Equals.
which is why your class's Equals method is not being called. To get
the behaviour you want you have a number of choices which include these
as I think the most obvious two:
- Implement a strongly-typed ArrayList for storing solely Person objects, that uses Person.Equals when doing an IndexOf operation;
- Use a different collection class, one that explicitly supports
the use of overriden Equals operators. For example, a Hashtable uses
the Equals implementation of the key objects, rather than always using Object.Equals
If you need wither or both of these options explained more, feel
free to ask :)
-- Larry Lard Replies to group please
Hi Larry,
thanks for the quick reply. You have confirmed my idea of what needs to
be done... I have created my own custom arraylist as you suggested, and
created a few 'wrapper' functions to store/access/search/retrieve items
stored in it regardless of class.
it is a shame, however, that the arraylist.index of class doesn't call the
overridden object.equals methods. Oh, well...
Regards,
John
"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. . JohnR wrote: Hi Larry,
> - Implement a strongly-typed ArrayList for storing solely Person > objects, that uses Person.Equals when doing an IndexOf operation;
I'm not exactly sure what you mean...
Every entry into the arraylist was of type PERSON. But, still, the indexof didn't call the object.equals in the Person class. I'm guessing that it is calling a object.equals method that is in the ArrayList class,
An ArrayList will *always* use Object.Equals (the Equals method defined in the base class Object) when checking for equality in its IndexOf method. That is just the way ArrayList is implemented.
so I tried to create a new class ARRAYLISTPERSON which inherits from arraylist. I then put the overrides overloads function equals(byval obj as object) as boolean in the arraylistperson class def'n and stored my PERSON instances in this class rather than the regular arraylist class. Unfortunately, when the indexof method was called, my Equals function was not called, so I'm still at a loss.
Supplying a new Equals method, wherever you do it, won't change ArrayList's behaviour - it will always use Object.Equals.
Do you have a small code example of how to override the
Object.equals method of an arraylist.index of?
What you need to change the behaviour of is the *IndexOf* method. Here is the general idea:
Public Class ArrayListOfPers on Inherits ArrayList
Public Shadows Function IndexOf(ByVal p As Person) As Integer Dim iterp As Person Dim i As Integer
For i = 0 To Me.Count - 1 iterp = DirectCast(Me(i ), Person) If p.Equals(iterp) Then Return i Next
Return -1 End Function End Class
Usage example:
Dim al As ArrayList = New ArrayList
al.Add(New Person(10)) al.Add(New Person(5))
MsgBox(al.Index Of(New Person(5))) 'this will show -1, because ArrayList uses Object.Equals to compare 'while performing the IndexOf method 'and Object.Equals requires reference equivalence
Dim alp As ArrayListOfPers on = New ArrayListOfPers on
alp.Add(New Person(10)) alp.Add(New Person(5))
MsgBox(alp.Inde xOf(New Person(5))) 'this will return 1, because ArrayListOfPers on uses Person.Equals 'while performing IndexOf(As Person)
**** PLEASE NOTE **** that this lacks robustness in a number of ways, and this is *not* how I would suggest implementing a strongly-typed collection in anything larger than a toy (ie personal-use only) application. The main problems are that: There is nothing to stop a careless client of ArrayListOfPers on adding a non-Person; the Item() property is still returned As Object so must be casted to Person; an ArrayListOfPers on can be casted down to ArrayList and lose all its custom behaviour. To reiterate, DON'T do custom collections in this way in seroius apps! This is just an example! You can find many posts on the correct ways to implement custom collections here in this group. Or start a new thread and ask, as that is a separate topic :) Thanks, John
"Larry Lard" <la*******@hotm ail.com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.com.. . > > JohnR wrote: >> 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 class where only
the > >> "searched" property has a value. I expected to get the index into > the >> arraylist where I could then get the entire class instance. > However, the >> 'indexof' is never calling my overloaded, overrides Equals method. > Here is >> the code snippet: >> >> Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As >> System.EventArg s) Handles MyBase.Load >> >> Dim PeopleCollectio n As New ArrayList >> >> Dim Customer As New Person >> Customer.Age = 25 >> Customer.IsMale = "Yes" >> >> PeopleCollectio n.Add(Customer) >> >> Dim y As New Person >> y.Age = 25 >> Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this always >> returns -1, meaning it didn't match anything >> End Sub >> >> Public Class Person >> Private _IsMale As String >> Private _age As Integer >> Public Overloads Overrides Function equals(ByVal obj As Object) As > Boolean >> If obj Is Nothing Then Return False >> If Me.GetType Is obj.GetType Then >> Return Me.Age = CType(obj, Person).Age >> End If >> End Function > [snip] >> >> In this example I wanted to get the instance of the PERSON whose age > is 25. >> If I put a breakpoint in my overloads overrides equals method, it > never >> breaks. My EQUALS method is not being called. >> >> Can anybody explain what is going on, and how I can get my indexof to > do >> what I need it to do? > > This is technically a feature not a bug (doesn't help I know), as the > behaviour is as documented. In the docs for ArrayList.Index Of, you will > see it says: > >>> > Remarks > ... > This method determines equality by calling Object.Equals. >>> > > which is why your class's Equals method is not being called. To get the > behaviour you want you have a number of choices which include these as > I think the most obvious two: > > - Implement a strongly-typed ArrayList for storing solely Person > objects, that uses Person.Equals when doing an IndexOf operation; > > - Use a different collection class, one that explicitly supports the > use of overriden Equals operators. For example, a Hashtable uses the > Equals implementation of the key objects, rather than always using > Object.Equals > > If you need wither or both of these options explained more, feel free > to ask :) > > -- > Larry Lard > Replies to group please >
John,
Your code works as expected! As Larry pointed out, ArrayList.Index Of uses
Object.Equals to match the items. Object.Equals is an overridable method, so
it behaves polymorphically across any type that overrides it. Your Person
type overrides Object.Equals. So ArrayList.Index Of will call Person.Equals.
Trying your code as posted returns 0 in VB.NET 2003!
To see that Person.Equals is actually getting called, simply place a
breakpoint on the Person.Equals method before calling
PeopleCollectio n.IndexOf.
Hope this helps
Jay
"JohnR" <Jo******@hotma il.com> wrote in message
news:HRkbe.5251 $WX.1041@trndny 01...
| 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 class where only the
| "searched" property has a value. I expected to get the index into the
| arraylist where I could then get the entire class instance. However, the
| 'indexof' is never calling my overloaded, overrides Equals method. Here
is
| the code snippet:
|
| Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
| System.EventArg s) Handles MyBase.Load
|
| Dim PeopleCollectio n As New ArrayList
|
| Dim Customer As New Person
| Customer.Age = 25
| Customer.IsMale = "Yes"
|
| PeopleCollectio n.Add(Customer)
|
| Dim y As New Person
| y.Age = 25
| Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this always
| returns -1, meaning it didn't match anything
| End Sub
|
| Public Class Person
| Private _IsMale As String
| Private _age As Integer
| Public Overloads Overrides Function equals(ByVal obj As Object) As Boolean
| If obj Is Nothing Then Return False
| If Me.GetType Is obj.GetType Then
| Return Me.Age = CType(obj, Person).Age
| End If
| End Function
| Public Property IsMale() As String
| Get
| Return _IsMale
| End Get
| Set(ByVal Value As String)
| _IsMale = Value
| End Set
| End Property
| Public Property Age() As Integer
| Get
| Return _age
| End Get
| Set(ByVal Value As Integer)
| _age = Value
| End Set
| End Property
| End Class
|
| In this example I wanted to get the instance of the PERSON whose age is
25.
| If I put a breakpoint in my overloads overrides equals method, it never
| breaks. My EQUALS method is not being called.
|
| Can anybody explain what is going on, and how I can get my indexof to do
| what I need it to do?
|
| Thanks, John
|
|
Larry,
| >>
| Remarks
| ...
| This method determines equality by calling Object.Equals.
| >>
|
| which is why your class's Equals method is not being called. To get the
| behaviour you want you have a number of choices which include these as
Err! Object.Equals is overridable, John overrode it. Overridable means the
method behaves polymorphically .
When ArrayList.Index Of calls Object.Equals, because John's Person.Equals
overrides it, Person.Equals will actually be called!
Using Shadows is anti-polymorphic. When ArrayList.Index Of calls
Object.Equals, if Person shadows Equals, then Object.Equals will be called &
not Person.Equals. Rarely do you want to use Shadows as they are
anti-polymorphic!
John's code as posted works as expected, I suspect there is another problem
going on.
Hope this helps
Jay
"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
|
| JohnR wrote:
| > 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 class where only the
|
| > "searched" property has a value. I expected to get the index into
| the
| > arraylist where I could then get the entire class instance.
| However, the
| > 'indexof' is never calling my overloaded, overrides Equals method.
| Here is
| > the code snippet:
| >
| > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
| > System.EventArg s) Handles MyBase.Load
| >
| > Dim PeopleCollectio n As New ArrayList
| >
| > Dim Customer As New Person
| > Customer.Age = 25
| > Customer.IsMale = "Yes"
| >
| > PeopleCollectio n.Add(Customer)
| >
| > Dim y As New Person
| > y.Age = 25
| > Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this always
| > returns -1, meaning it didn't match anything
| > End Sub
| >
| > Public Class Person
| > Private _IsMale As String
| > Private _age As Integer
| > Public Overloads Overrides Function equals(ByVal obj As Object) As
| Boolean
| > If obj Is Nothing Then Return False
| > If Me.GetType Is obj.GetType Then
| > Return Me.Age = CType(obj, Person).Age
| > End If
| > End Function
| [snip]
| >
| > In this example I wanted to get the instance of the PERSON whose age
| is 25.
| > If I put a breakpoint in my overloads overrides equals method, it
| never
| > breaks. My EQUALS method is not being called.
| >
| > Can anybody explain what is going on, and how I can get my indexof to
| do
| > what I need it to do?
|
| This is technically a feature not a bug (doesn't help I know), as the
| behaviour is as documented. In the docs for ArrayList.Index Of, you will
| see it says:
|
| >>
| Remarks
| ...
| This method determines equality by calling Object.Equals.
| >>
|
| which is why your class's Equals method is not being called. To get the
| behaviour you want you have a number of choices which include these as
| I think the most obvious two:
|
| - Implement a strongly-typed ArrayList for storing solely Person
| objects, that uses Person.Equals when doing an IndexOf operation;
|
| - Use a different collection class, one that explicitly supports the
| use of overriden Equals operators. For example, a Hashtable uses the
| Equals implementation of the key objects, rather than always using
| Object.Equals
|
| If you need wither or both of these options explained more, feel free
| to ask :)
|
| --
| Larry Lard
| Replies to group please
|
Jay B. Harlow [MVP - Outlook] wrote: Larry, | >> | Remarks | ... | This method determines equality by calling Object.Equals. | >> | | which is why your class's Equals method is not being called. To get
the | behaviour you want you have a number of choices which include these
as Err! Object.Equals is overridable, John overrode it. Overridable
means the method behaves polymorphically .
When ArrayList.Index Of calls Object.Equals, because John's
Person.Equals overrides it, Person.Equals will actually be called!
Using Shadows is anti-polymorphic. When ArrayList.Index Of calls Object.Equals, if Person shadows Equals, then Object.Equals will be
called & not Person.Equals. Rarely do you want to use Shadows as they are anti-polymorphic!
John's code as posted works as expected, I suspect there is another
problem going on.
So it does. Guess I should have checked that before going off on one,
to avoid looking silly now. Oh well.
--
Larry Lard
Replies to group please
In article <HRkbe.5251$WX. 1041@trndny01>, JohnR wrote: 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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the code snippet:
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim PeopleCollectio n As New ArrayList
Dim Customer As New Person Customer.Age = 25 Customer.IsMale = "Yes"
PeopleCollectio n.Add(Customer)
Dim y As New Person y.Age = 25 Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this always returns -1, meaning it didn't match anything End Sub
Public Class Person Private _IsMale As String Private _age As Integer Public Overloads Overrides Function equals(ByVal obj As Object) As Boolean If obj Is Nothing Then Return False If Me.GetType Is obj.GetType Then Return Me.Age = CType(obj, Person).Age End If End Function Public Property IsMale() As String Get Return _IsMale End Get Set(ByVal Value As String) _IsMale = Value End Set End Property Public Property Age() As Integer Get Return _age End Get Set(ByVal Value As Integer) _age = Value End Set End Property End Class
In this example I wanted to get the instance of the PERSON whose age is 25. If I put a breakpoint in my overloads overrides equals method, it never breaks. My EQUALS method is not being called.
Can anybody explain what is going on, and how I can get my indexof to do what I need it to do?
Thanks, John
IMHO, this is a bug... The problem is that it seems that the arraylist
Index of operator does something like the following (obviously aircode):
For Each obj As Object in List
value.Equals (obj)
Next
Which is backwards from what you would expect:
For Each obj As Object in List
obj.Equals (value)
Next
So, to get around this you need create a custom collection class that
overrides IndexOf... Here is my C# implementation of a class I did:
public int IndexOf(string value)
{
int index = -1;
// HACK: Get around strange arraylist.index of method implementation. ..
for (int i = 0; i < this.InnerList. Count; i++)
{
if (this.InnerList[i].Equals(value))
{
index = i;
break;
}
}
return index;
}
The funny thing is that Mono did it the right way... But, broke it to
be like .NET in response to a post I made on the mono mailing list :)
--
Tom Shelton [MVP]
Tom,
| IMHO, this is a bug... The problem is that it seems that the arraylist
| Index of operator does something like the following (obviously aircode):
I don't see there is a Bug in John's code. If you try his code it works as
expected in VS.NET 2003 (.NET 1.1)!
| For Each obj As Object in List
| value.Equals (obj)
| Next
|
| Which is backwards from what you would expect:
|
| For Each obj As Object in List
| obj.Equals (value)
| Next
Object.Equals is defined such that "if x.Equals(y) return the same value as
y.Equals(x)" so I don't really see a problem or bug in your sample either! http://msdn.microsoft.com/library/de...ualstopic1.asp
Hope this helps
Jay
"Tom Shelton" <to*@YOUKNOWTHE DRILLmtogden.co m> wrote in message
news:uz******** ******@TK2MSFTN GP14.phx.gbl...
| In article <HRkbe.5251$WX. 1041@trndny01>, JohnR wrote:
| > 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 class where only the
| > "searched" property has a value. I expected to get the index into the
| > arraylist where I could then get the entire class instance. However,
the
| > 'indexof' is never calling my overloaded, overrides Equals method. Here
is
| > the code snippet:
| >
| > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
| > System.EventArg s) Handles MyBase.Load
| >
| > Dim PeopleCollectio n As New ArrayList
| >
| > Dim Customer As New Person
| > Customer.Age = 25
| > Customer.IsMale = "Yes"
| >
| > PeopleCollectio n.Add(Customer)
| >
| > Dim y As New Person
| > y.Age = 25
| > Dim x As Integer = PeopleCollectio n.IndexOf(y) 'this always
| > returns -1, meaning it didn't match anything
| > End Sub
| >
| > Public Class Person
| > Private _IsMale As String
| > Private _age As Integer
| > Public Overloads Overrides Function equals(ByVal obj As Object) As
Boolean
| > If obj Is Nothing Then Return False
| > If Me.GetType Is obj.GetType Then
| > Return Me.Age = CType(obj, Person).Age
| > End If
| > End Function
| > Public Property IsMale() As String
| > Get
| > Return _IsMale
| > End Get
| > Set(ByVal Value As String)
| > _IsMale = Value
| > End Set
| > End Property
| > Public Property Age() As Integer
| > Get
| > Return _age
| > End Get
| > Set(ByVal Value As Integer)
| > _age = Value
| > End Set
| > End Property
| > End Class
| >
| > In this example I wanted to get the instance of the PERSON whose age is
25.
| > If I put a breakpoint in my overloads overrides equals method, it never
| > breaks. My EQUALS method is not being called.
| >
| > Can anybody explain what is going on, and how I can get my indexof to do
| > what I need it to do?
| >
| > Thanks, John
| >
| >
|
| IMHO, this is a bug... The problem is that it seems that the arraylist
| Index of operator does something like the following (obviously aircode):
|
| For Each obj As Object in List
| value.Equals (obj)
| Next
|
| Which is backwards from what you would expect:
|
| For Each obj As Object in List
| obj.Equals (value)
| Next
|
| So, to get around this you need create a custom collection class that
| overrides IndexOf... Here is my C# implementation of a class I did:
|
| public int IndexOf(string value)
| {
| int index = -1;
|
| // HACK: Get around strange arraylist.index of method implementation. ..
| for (int i = 0; i < this.InnerList. Count; i++)
| {
| if (this.InnerList[i].Equals(value))
| {
| index = i;
| break;
| }
| }
|
| return index;
| }
|
| The funny thing is that Mono did it the right way... But, broke it to
| be like .NET in response to a post I made on the mono mailing list :)
|
| --
| Tom Shelton [MVP] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 _name;
|
by: Rubbrecht Philippe |
last post by:
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?!
|
by: Mykl |
last post by:
I'm trying to derive a class from ArrayList. When I try to use the
Overrides keyword on the Item property, the IDE underlines Item in my
Function definition and says "Item conflicts with a property by the same
name declared in ArrayList"... what gives? Am I out to lunch? I should
add that I'm still a little new to OO and inheritance, polymorphism, etc,
and would really like to understand this...
Here's my code:
Public Class...
|
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 == and != opperators. Am
I missing something or when would one want to have different implementations
for Equals and ==?
--Ken
|
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly.
In this article I'll take a look at the equals and hashCode methods.
...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |