VB.NET 在数组列表中查找值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23123103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET Find value in arraylist
提问by Luiey
I have this structure:
我有这个结构:
Public strcMyInfo As DISPLAYDIRECTORY
Public arrDirectory As ArrayList
Public Structure DISPLAYDIRECTORY
Dim strdirno As String
Dim strdirname As String
Dim strdirdetails As String
Dim strcategory As String
Dim strwebsite As String
Dim strphoneno As String
End Structure
I will do a query to database and add structure strcMyInfoto arrDirectory. The arrDirectorywill hold data which contains the strcMyInfodata let's say 10 index. For example, the value of arrDirectory.Item(6).strdirnameis G2000. How can I loop through the arraylist to find the value of G2000 together with strdirno, strdirdetails,strcategory,strwebsite and strphoneno?
我将对数据库进行查询并将结构添加strcMyInfo到arrDirectory. 在arrDirectory其中包含将持有数据strcMyInfo的数据让我们说10指数。例如,的值为arrDirectory.Item(6).strdirnameG2000。如何循环遍历数组列表以找到 G2000 的值和strdirno, strdirdetails,strcategory,strwebsite and strphoneno?
I have search for internet but they only look for a 1 value when adding as example below:
我搜索了互联网,但在添加以下示例时,他们只查找 1 值:
myAL.Add("the")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")
But my code will be like this:
但我的代码将是这样的:
If (rdr.HasRows()) Then
arrDirectory = Nothing
arrDirectory = New ArrayList
While rdr.Read
With strcSearchDir
If Not IsDBNull("dirno") Then
.strdirno = (rdr("dirno"))
Else
.strdirno = "N/A"
End If
If Not IsDBNull("dirname") Then
.strdirname = (rdr("dirname"))
Else
.strdirname = "N/A"
End If
If Not IsDBNull("dirdetails") Then
.strdirdetails = (rdr("dirdetails"))
Else
.strdirdetails = "N/A"
End If
If Not IsDBNull("category") Then
.strcategory = (rdr("category"))
Else
.strcategory = "N/A"
End If
If Not IsDBNull("website") Then
.strwebsite = (rdr("website"))
Else
.strwebsite = "N/A"
End If
If Not IsDBNull("phoneno") Then
.strphoneno = (rdr("phoneno"))
Else
.strphoneno = "N/A"
End If
End With
arrDirectory.Add(strcSearchDir)
End While
Return True
Else
Return False
End If
Below are code to find the string but it stop there because i don't know how to continue:
下面是查找字符串的代码,但它停在那里,因为我不知道如何继续:
Private Sub GetMyDetails(ByVal strLabel As Label)
Dim obj As Object
Try
If strLabel.Content <> String.Empty Then
For Each obj In arrDirectory
If arrDirectory.IndexOf(obj) = strLabel.Content Then
End If
Next
End If
Catch ex As Exception
End Try
End Sub
If someone know how to use the indexof in arraylist, please guide me. Thanks
如果有人知道如何使用arraylist中的indexof,请指导我。谢谢
回答by jmcilhinney
You should not be using IndexOfat all. The whole point of a For Eachloop is to access the items themselves and you then want to get the item that has a particular value for a particular field/property.
你根本不应该使用IndexOf。一个整点For Each循环是访问项目本身,然后你想获得具有特定字段/属性的特定值的项目。
Private Function GetMyDetails(ByVal strDirName As String) As DISPLAYDIRECTORY
Dim obj As DISPLAYDIRECTORY
Try
If strDirName <> String.Empty Then
For Each item As DISPLAYDIRECTORY In arrDirectory
If item.strdirname = strDirName Then
obj = item
Exit For
End If
Next
End If
Catch ex As Exception
End Try
Return obj
End Sub

