我想制作一个集合来提供数据 例如:
Dim b As New Collection
colb = New Collection
b.Add("system", "1001", "SYSTEM")
b.Add("network", "1002", "NETWORKA")
b.Add("networksecond", "1010", "NETWORKB")
colb.Add(b, "list")
我正在寻找一个从这个集合中获取数据的函数: 我想,根据ID(第二个数字)得到第一个和第三个值 因此,如果我搜索1010,我需要具有值Network和NETWORKA
答案 0 :(得分:2)
不,严重的是,请考虑使用Dictionary
而不是旧的旧版Collection类。看看泛型的美感和强烈的打字:
Dim dic As New Dictionary(Of Integer, Tuple(Of String, String))
dic.Add(1001, Tuple.Create("system", "SYSTEM"))
dic.Add(1002, Tuple.Create("network", "NETWORKA"))
dic.Add(1010, Tuple.Create("networksecond", "NETWORKB"))
' Search
Dim t As Tuple(Of String, String) = Nothing
If dic.TryGetValue(1002, t) Then
Console.WriteLine(t.Item1) ' prints "network"
Console.WriteLine(t.Item2) ' prints "NETWORKA"
End If
只要您有两个以上的值,我建议您使用专门的类而不是元组来提高可读性。
答案 1 :(得分:0)
此外,您只需使用List(Of T)
即可。在大多数情况下,这就足够了。字典适用于通过单个键快速搜索长列表。
'declare model
Public Class NetworkModel
Public Property Id As Integer
Public Property Name1 As String
Public Property Name2 As String
End Class
' load list of models
Private _modelList As New List(Of NetworkModel)()
.......
' search using LINQ
Dim model As NetworkModel = _modelList.FirstOrDefault(Function(m) m.Id = 1001)
If model IsNot Nothing Then . . . . .