Generic List

S

shapper

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?

Thanks,

Miguel
 
M

mark4asp

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?

Thanks,

Miguel

I think this is how you'd do it in C#:

Row rw = Rows.Find(delegate(Row aRow)
{
return (aRow.Name == "Home"));
}
);

string someContent = rw.Content;
 
S

shapper

I think this is how you'd do it in C#:

Row rw = Rows.Find(delegate(Row aRow)
{
return (aRow.Name == "Home"));
}
);

string someContent = rw.Content;

This is not working.

I tried to create a class as follows:

' BoxPredicates
Public Class BoxPredicates
Public Shared Function FindRowByName(ByVal row As BoxRows.Row, ByVal
rowName As String) As Boolean
Return String.Compare(row.Name, rowName)
End Function ' FindRowByName
End Class ' BoxPredicates

However, I have no been able to use this in List.Find.

Could someone, please, help me out?

Thanks,
Miguel
 
R

Roland Dick

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top