重写Equals方法以在ArrayList中查找IndexOf项 [英] Overriding the Equals method to find the IndexOf an item in an ArrayList

查看:95
本文介绍了重写Equals方法以在ArrayList中查找IndexOf项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

根据文档,我读了ArrayList.IndexOf方法使用

Object.Equals方法循环遍历其列表中的项目并找到返回True项的

第一个索引。


因此,在项目的类定义中覆盖Equals方法

我放入ArrayList应该让IndexOf方法使用我的版本的

Equals方法?!


但是,这似乎不起作用。

在标准的VB.NET控制台应用程序中使用以下代码,返回-1作为IndexOf方法的结果

a。我在这里做错了什么?!


祝你好运,

Philippe Rubbrecht


模块主页

Sub Main()


''创建ArrayList并添加一些项目。


Dim lst As ArrayList = New ArrayList


Dim itm作为ListItem

itm = New ListItem(" BE"," Belgium"):lst。添加(itm)


itm =新ListItem(DE,德国):lst.Add(itm)


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


''现在尝试使用重载的Equals方法查找项的索引。


Dim key As String =" DE"


''现在输出最后添加的项目的索引。


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


''暂停。


System.Console.ReadLine()


End Sub


结束模块


公共类ListItem


私人mstrKey As字符串


私有mstrDescription字符串


公共子新(ByVal键为字符串,ByVal描述为字符串)

Me.mstrKey =钥匙


Me.mstrDescription =描述


结束子


公共属性键()为字符串


获取


返回Me.mstrKey


结束获取


设置(ByVal值为字符串)


Me.mstrKey =价值


结束集


结束物业


公共财产描述()作为字符串


获取


返回Me.mstrDescription


结束获取


设置(ByVal Value As String)


Me.mstrDescription =价值


结束套件


结束财产

公共过载覆盖功能等als(ByVal obj As Object)As Boolean


如果obj.GetType是GetType(ListItem)那么


返回CType(obj,ListItem) .Key.Equals(Me.Key)


ElseIf obj.GetType是GetType(String)然后


返回CType(obj,String) .Equals(Me.Key)


否则


返回错误


结束如果


结束功能


结束班

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

推荐答案

也许我错过了一些东西,但我确实运行了你的代码,它返回True

(-1),因为它找到了键(并且你返回一个布尔值),因为它

测试为在Type中是ListItem,你期望返回什么?


问候 - OHM


Rubbrecht Philippe写道:
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:

根据文档,我读到了ArrayList.IndexOf方法使用了Object.Equals方法来遍历其列表中的项目并找到了他是第一个返回True的项目的索引。

因此,在我定义的项目定义中覆盖我在ArrayList中的项目应该使IndexOf方法使用
我的版本的Equals方法?!

然而,这似乎不起作用。
在标准VB.NET控制台应用程序中使用以下代码返回
-1作为结果IndexOf方法。我做错了什么?

致以诚挚的问候,
Philippe Rubbrecht

模块主要

Sub Main( )

''创建ArrayList并添加一些项目。

Dim lst As ArrayList = New ArrayList

将它们视为ListItem

itm = New ListItem(BE,比利时):lst.Add(itm)

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

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

''现在尝试查找使用重载的Equals
方法的项目索引。

Dim key As String =" DE"

''现在输出最后一项的索引添加。

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

''等待。

System.Console.ReadLine()

结束子

结束模块
<公共类ListItem

P rivate mstrKey As String

私有mstrDescription As String

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

Me.mstrKey =密钥

Me.mstrDescription = description

End Sub

公共属性键()为字符串

获取
结束获取

设置(ByVal值为字符串)

Me.mstrKey = Value

结束集

结束财产

公共财产描述()作为字符串

获取

返回Me.mstrDescription

结束获取

设置(ByVal值为字符串)

Me.mstrDescription = Value

结束集

结束属性

公共重载覆盖函数等于(ByVal obj As Object)As
Boolean

如果obj.GetType是GetType(ListItem)然后

返回CType(obj,ListItem).Key.Equals(Me.Key)

ElseIf obj.GetType是GetType(String)然后

返回CType (obj,String).Equals(Me.Key)

其他

返回错误

结束如果

结束功能

结束类
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



IndexOf方法应该返回返回的第一个项目的索引

对于Equals方法为真。在示例中,IndexOf方法应返回

1而不是-1(这是未找到项目时的默认答案)。

One Handed Man <博**** @ Duck.net>在消息中写道

news:bq ********** @ hercules.btinternet.com ...
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...
也许我错过了什么,但我确实跑了你的代码和它返回True
( - 1),因为它找到了键(并且你返回一个布尔值)因为它在Type中测试为ListItem,你期望返回什么?问候 - OHM

Rubbrecht Philippe写道:
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:
你好,

根据文档,我读了ArrayList.IndexOf方法使用Object.Equals方法遍历其列表中的项目,并找到返回True的项目的第一个索引。

因此,重写Equals方法in我在ArrayList中放置的项目的类定义应该使IndexOf方法使用我的版本的Equals方法?!

但是,这似乎不起作用。
在标准VB.NET控制台应用程序中使用以下代码返回
-1作为I的结果ndexOf方法。我做错了什么?

致以诚挚的问候,
Philippe Rubbrecht

模块主要

Sub Main( )

''创建ArrayList并添加一些项目。

Dim lst As ArrayList = New ArrayList

将它们视为ListItem

itm = New ListItem(BE,比利时):lst.Add(itm)

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

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

''现在尝试查找使用重载的Equals
方法的项目索引。

Dim key As String =" DE"

''现在输出最后一项的索引添加。

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

''等待。

System.Console.ReadLine()

结束子

结束模块
<公共类ListItem

P rivate mstrKey As String

私有mstrDescription As String

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

Me.mstrKey =密钥

Me.mstrDescription = description

End Sub

公共属性键()为字符串

获取
结束获取

设置(ByVal值为字符串)

Me.mstrKey = Value

结束集

结束财产

公共财产描述()作为字符串

获取

返回Me.mstrDescription

结束获取

设置(ByVal值为字符串)

Me.mstrDescription = Value

结束集

结束属性

公共重载覆盖函数等于(ByVal obj As Object)As
Boolean

如果obj.GetType是GetType(ListItem)然后

返回CType(obj,ListItem).Key.Equals(Me.Key)

ElseIf obj.GetType是GetType(String)然后

返回CType (obj,String).Equals(Me.Key)

其他

返回错误

结束如果

结束功能

End Class
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




好的,我现在看到错误。


正在传递字符串的索引(不是要搜索的对象

以下修改后的代码用于测试。我还将ListView类更改为ListView1。


OHM


''创建ArrayList并添加一些项目。


Dim lst As ArrayList = New ArrayList


Dim itm1,itm2,itm3 As ListItem1

itm1 = New ListItem1( BE,比利时):lst.Add(itm1)


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


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


''现在尝试使用重载的Equals方法找到项目的索引。


''*****注意现在将key设置为item2 - 此搜索称为

重载和覆盖函数等于****


Dim key As ListItem1 = itm2


''现在输出索引添加的最后一项。


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


''等待。


System.Console.ReadLine()


Rubbrecht Philippe写道:
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:
IndexOf方法应返回
为Equals方法返回True的第一个项的索引。在示例中,IndexOf
方法应返回1而不是-1(这是未找到项目时的默认答案)。

One Handed Man <博**** @ Duck.net>在消息中写道
新闻:bq ********** @ hercules.btinternet.com ...
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...
也许我错过了什么,但我确实运行了你的代码而它是
返回True(-1),因为它找到了键(并且你正在返回一个布尔值),因为它在Type中测试为ListItem,你希望返回什么
?问候 - OHM

Rubbrecht Philippe写道:
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:
你好,

根据文档,我读了ArrayList.IndexOf方法使用Object.Equals方法遍历其列表中的项目,并找到返回True的项目的第一个索引。

因此,重写Equals方法in我在ArrayList中放置的项目的类定义应该使IndexOf方法使用我的版本的Equals方法?!

但是,这似乎不起作用。
在标准VB.NET控制台应用程序中使用以下代码
返回-1作为结果IndexOf方法。我在做什么
错了吗?!

致以诚挚的问候,
Philippe Rubbrecht

模块主要

Sub Main( )

''创建ArrayList并添加一些项目。

Dim lst As ArrayList = New ArrayList

将它们视为ListItem

itm = New ListItem(BE,比利时):lst.Add(itm)

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

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

''现在尝试查找使用重载的Equals
方法的项目索引。

Dim key As String =" DE"

''现在输出最后一项的索引添加。

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

''等待。

System.Console.ReadLine()

结束子

结束模块
<公共类ListItem

P rivate mstrKey As String

私有mstrDescription As String

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

Me.mstrKey =密钥

Me.mstrDescription = description

End Sub

公共属性键()为字符串

获取
结束获取

设置(ByVal值为字符串)

Me.mstrKey = Value

结束集

结束财产

公共财产描述()作为字符串

获取

返回Me.mstrDescription

结束获取

设置(ByVal值为字符串)

Me.mstrDescription = Value

结束集

结束属性

公共重载覆盖函数等于(ByVal obj As Object)As
Boolean

如果obj.GetType是GetType(ListItem)然后

返回CType(obj,ListItem).Key.Equals(Me.Key)

ElseIf obj.GetType是GetType(String)然后

返回CType (obj,String).Equals(Me.Key)

其他

返回错误

结束如果

结束功能

End Class
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



这篇关于重写Equals方法以在ArrayList中查找IndexOf项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆