Question about generic Finds

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    Question about generic Finds

    I'm studying for a cert and hacking some code. I have a generic List(of T)
    declared which contains a number of instances of a class. What I would like
    to do is setup a search predicate (?) on Find that would return a class
    instance with a unique property value. (But I'm not clear on the concept.)

    What I have so far is a function within the the class defined as: Private
    Function test(ByVal x As Class1) As Boolean. The compiler will then accept
    Class1Instance = List1.Find(Addr essOf test).

    I suppose I could define a variable outside the test function but that seems
    sloppy. How do you set up a delegate so that you can pass a search parameter
    directly from the Find itself? (Can you?)


  • Branco Medeiros

    #2
    Re: Question about generic Finds

    B. Chernick wrote:
    I'm studying for a cert and hacking some code.  I have a generic List(of T)
    declared which contains a number of instances of a class.  What I wouldlike
    to do is setup a search predicate (?) on Find that would return a class
    instance with a unique property value.  (But I'm not clear on the concept.)
    >
    What I have so far is a function within the the class defined as: Private
    Function test(ByVal x As Class1) As Boolean.  The compiler will then accept
    Class1Instance = List1.Find(Addr essOf test).  
    >
    I suppose I could define a variable outside the test function but that seems
    sloppy.  How do you set up a delegate so that you can pass a search parameter
    directly from the Find itself?  (Can you?)
    One way to do it is to define the function in a separate class, one to
    which you could pass the search parameter.

    <example>
    Class NameFinder
    Private mName As String
    Public Sub New(NameToFind As String)
    mName = NameToFind
    End Sub

    Function FindByName(Item As Class1) As Boolean
    Return Item.Name = mName
    End Function
    End Class

    '...

    Dim F As New NameFinder("Joh n Doe")
    ClassInstance = List1.Find(Addr essOf F.FindByName)
    </example>

    Hope this helps.

    (and good luck on the exam)

    Branco.

    Comment

    • =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

      #3
      Re: Question about generic Finds

      Actually that's approximately what I did. Thanks.

      (It just seems a bit non-intuitive, not being able to pass the test
      parameter directly to the Find command.)

      "Branco Medeiros" wrote:
      B. Chernick wrote:
      I'm studying for a cert and hacking some code. I have a generic List(of T)
      declared which contains a number of instances of a class. What I would like
      to do is setup a search predicate (?) on Find that would return a class
      instance with a unique property value. (But I'm not clear on the concept.)

      What I have so far is a function within the the class defined as: Private
      Function test(ByVal x As Class1) As Boolean. The compiler will then accept
      Class1Instance = List1.Find(Addr essOf test).

      I suppose I could define a variable outside the test function but that seems
      sloppy. How do you set up a delegate so that you can pass a search parameter
      directly from the Find itself? (Can you?)
      >
      One way to do it is to define the function in a separate class, one to
      which you could pass the search parameter.
      >
      <example>
      Class NameFinder
      Private mName As String
      Public Sub New(NameToFind As String)
      mName = NameToFind
      End Sub
      >
      Function FindByName(Item As Class1) As Boolean
      Return Item.Name = mName
      End Function
      End Class
      >
      '...
      >
      Dim F As New NameFinder("Joh n Doe")
      ClassInstance = List1.Find(Addr essOf F.FindByName)
      </example>
      >
      Hope this helps.
      >
      (and good luck on the exam)
      >
      Branco.
      >

      Comment

      Working...