我有这个结构:
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
我将对数据库进行查询,并将结构strcMyInfo
添加到arrDirectory
。 arrDirectory
将保存包含strcMyInfo
数据的数据,即10个索引。例如,arrDirectory.Item(6).strdirname
的值是G2000。如何循环遍历arraylist以找到G2000的值以及strdirno, strdirdetails,strcategory,strwebsite and strphoneno
?
我已经搜索了互联网,但在添加以下示例时,他们只查找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")
但我的代码将是这样的:
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
下面是找到字符串的代码,但它停在那里因为我不知道如何继续:
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
如果有人知道如何在arraylist中使用indexof,请指导我。谢谢
答案 0 :(得分:1)
您根本不应该使用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