-1

I want to make a collection to have data available Example:

    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")

im looking for a function to get data from this collection: I want to, based on the ID (Second number) get the first and third value So if I search for 1010, I need to have the value Network and NETWORKA

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Menno
  • 21
  • 1
  • 6

2 Answers2

2

VB6 called, they want their Collection back.

No, seriously, please consider using a Dictionary instead of the old, legacy Collection class. Behold the beauty of generics and strong typing:

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

As soon as you have more than two values, I suggest that you use a specialized class instead of a Tuple to increase readability.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

Also, you can simply use List(Of T). In most cases this is enough. Dictionary is good for fast search out long list by a single key.

'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 . . . . .
T.S.
  • 18,195
  • 11
  • 58
  • 78