List.FindAll question (new to .net 2.0) please help !!!! URGENT !!!

 

I've found several examples of how to use the findall method on the list class with a hard coded value.

 

How can I do this with a dynamic assigned variable.

I saw examples of implementing some sort of interface...

But personally I think that's a very difficult method... and ... that the FindAll method accepting only hard coded values is not for to much use right

hard coding    seems like... elementary school programming.

what can I do in this case... please some body help me...

I'm really desesperate.

best regards

Elias.




Answer this question

List.FindAll question (new to .net 2.0) please help !!!! URGENT !!!

  • Ken Sykora7765

    here's one way to do it (i typed this code directly into the post, so the syntax may not be exactly right):

    Public Sub FindInList(lst As List(Of String), find As String) As String()
        toFind = find
        Return lst.FindAll(AddressOf IsMatch)
    End Sub

    Dim toFind As String
    Public Function IsMatch(compare As String) As Boolean
        Return compare = toFind
    End Function



  • tracy97214

    Is the list class you're using the System.Generic.List(of T) class This is a part of the new generics feature. This feature was designed for a different scenario than dynamically assigned variables. And should be thought as separate. Specifically, when searching dynamically typed variables, there's a huge speed penalty since the type's members must be bound when running the code instead of during compilation.

    Generics are meant to be used to create fast highly optimized "generic" libraries which are then customized by the user by specifying the type when creating instances of the variables. Think of Generics as a hacking the language for speed :)

    ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vbalr/html/89f771d9-ecbb-4737-88b8-116b63c6cf4d.htm

    When using the Generics class List(of T) the user gives up a little flexibility in assigning the variable types so they can get more speed.

    Okay, if you just want the code to work, and are trying to put different types of data into the a List, you can use the old "Object" type for flexibilty, but you're going to sacrifice speed. The following code will work, but it's ugly and will not be performant.

    Imports System.drawing

    Module mod1

    Sub main()

    ' To find the first Point structure for which X > 200 a delegate

    ' that represents the GreaterThan200 method to the Shared

    ' FindAll method of the collection class.

    Dim Cltn As New Generic.List(Of Object)

    Cltn.Add(New Point(100, 200))

    Cltn.Add(New Point(150, 250))

    Cltn.Add(New Point(250, 275))

    'add a collection element of a very different type.

    Cltn.Add("string")

    'Get Point results

    Dim results As Object = Cltn.FindAll(AddressOf PointGreaterThan200)

    For Each p As Object In results

    MsgBox(p.ToString)

    Next

    End Sub

    ' This method implements the test condition for the FindAll

    ' method.

    Private Function PointGreaterThan200(ByVal p As Object) As Boolean

    Try

    If p.X > 200 Then

    Return True

    Else

    Return False

    End If

    Catch ex As MissingMemberException

    'If the type passed is does not have a member "X" it will receive an MissingMemberException.

    Return False

    End Try

    End Function

    End Module



  • List.FindAll question (new to .net 2.0) please help !!!! URGENT !!!