shapper said:
Hello,
I have a generic list named Rows:
Dim a As New Generic.List(Of Row)
Row is a structure which has 2 properties: Name and Content.
Is it possible, without a for loop, to find a Row in Rows which Name
is "Home" and get its content?
Hi shapper,
you could use the Find method with a suitable predicate as in (here
strings, but could be your datatype Row):
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Compsognathus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Oviraptor")
dinosaurs.Add("Velociraptor")
dinosaurs.Add("Deinonychus")
dinosaurs.Add("Dilophosaurus")
dinosaurs.Add("Gallimimus")
dinosaurs.Add("Triceratops")
Console.WriteLine(vbLf & _
"Equals: {0}", _
dinosaurs.Find(AddressOf Equals))
...
Private Shared Function Equals(ByVal s As String) _
As Boolean
If ("Velociraptor" = s) Then
Return True
Else
Return False
End If
End Function
But I doubt there is anything else happening than a for loop.
So, the first thing that comes to mind for this is a hashtable (generic:
Dictionary). Can you use that instead of a list? (The hashtable will NOT
keep the order of the items!)
If not, maybe you can derive from list and have a hashtable in your
class, which references the entries. You'll have to keep both list and
hashtable in sync though.
Hope this helps,
Cheers,
Roland