Find in Collection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joerg Battermann

    Find in Collection

    Hello there,

    I have a custom type defined via

    [...]
    Public Class Requirement
    Public IDNumber As Integer
    Public Name As String
    Public Description As String
    Public VersionPlanAttr ibutes As New _
    System.Collecti ons.Generic.Lis t(Of Attribute)
    Public Traces As New System.Collecti ons.Generic.Lis t(Of Trace)
    Public Checked As Boolean = False
    End Class
    [...]

    and want to find one particular item (identified via IDNumber) in a
    filled collection of these items declared via

    [...]
    Public ERSRequirements As New System.Collecti ons.Generic.Lis t(Of_
    Requirement)
    [...]


    ... is there any quick way to do it or would I have to loop through the
    collection?


    Best Regards and thanks,
    -Joerg
  • Herfried K. Wagner [MVP]

    #2
    Re: Find in Collection

    Joerg,

    "Joerg Battermann" <jb@justbe.co m> schrieb:[color=blue]
    > I have a custom type defined via
    >
    > [...]
    > Public Class Requirement
    > Public IDNumber As Integer
    > Public Name As String
    > Public Description As String
    > Public VersionPlanAttr ibutes As New _
    > System.Collecti ons.Generic.Lis t(Of Attribute)
    > Public Traces As New System.Collecti ons.Generic.Lis t(Of Trace)
    > Public Checked As Boolean = False
    > End Class
    > [...]
    >
    > and want to find one particular item (identified via IDNumber) in a filled
    > collection of these items declared via
    >
    > [...]
    > Public ERSRequirements As New System.Collecti ons.Generic.Lis t(Of_
    > Requirement)
    > [...]
    >
    >
    > .. is there any quick way to do it or would I have to loop through the
    > collection?[/color]

    \\\
    Public Class Form1
    Private Sub Test()
    Dim x As New List(Of Person)
    x.Add(New Person("Peter", 1099))
    x.Add(New Person("Frank", 342234))
    x.Add(New Person("Fergus" , 23423))
    x.Add(New Person("Bill", 234))
    Dim p As Person = _
    x.Find(New Predicate(Of Person)(Address Of GetFrank))
    MsgBox(p.Name)
    End Sub

    Private Function GetFrank( _
    ByVal obj As Person _
    ) As Boolean
    Return obj.Name = "Frank"
    End Function
    End Class

    Public Class Person
    Public Sub New(ByVal Name As String, ByVal Salary As Double)
    Me.Name = Name
    Me.Salary = Salary
    End Sub

    Public Name As String
    Public Salary As Double
    End Class
    ///

    Collections are unsorted, thus runtime is in O(n) if the collection contains
    n items.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Joerg Battermann

      #3
      Re: Find in Collection

      Hey there,

      thanks for the quick reply, however is it possible to define a name in
      your example using the Predicate(Of Person)(Address Of GetFrank)?

      I am calling a function with an IDNumber as variable and it shall find
      me the one Requirement that has that particular IDNumber... and return
      it or do something with it


      does this make sense?

      -j

      Herfried K. Wagner [MVP] wrote:[color=blue]
      > Joerg,
      >
      > "Joerg Battermann" <jb@justbe.co m> schrieb:[color=green]
      >> I have a custom type defined via
      >>
      >> [...]
      >> Public Class Requirement
      >> Public IDNumber As Integer
      >> Public Name As String
      >> Public Description As String
      >> Public VersionPlanAttr ibutes As New _
      >> System.Collecti ons.Generic.Lis t(Of Attribute)
      >> Public Traces As New System.Collecti ons.Generic.Lis t(Of Trace)
      >> Public Checked As Boolean = False
      >> End Class
      >> [...]
      >>
      >> and want to find one particular item (identified via IDNumber) in a
      >> filled collection of these items declared via
      >>
      >> [...]
      >> Public ERSRequirements As New System.Collecti ons.Generic.Lis t(Of_
      >> Requirement)
      >> [...]
      >>
      >>
      >> .. is there any quick way to do it or would I have to loop through the
      >> collection?[/color]
      >
      > \\\
      > Public Class Form1
      > Private Sub Test()
      > Dim x As New List(Of Person)
      > x.Add(New Person("Peter", 1099))
      > x.Add(New Person("Frank", 342234))
      > x.Add(New Person("Fergus" , 23423))
      > x.Add(New Person("Bill", 234))
      > Dim p As Person = _
      > x.Find(New Predicate(Of Person)(Address Of GetFrank))
      > MsgBox(p.Name)
      > End Sub
      >
      > Private Function GetFrank( _
      > ByVal obj As Person _
      > ) As Boolean
      > Return obj.Name = "Frank"
      > End Function
      > End Class
      >
      > Public Class Person
      > Public Sub New(ByVal Name As String, ByVal Salary As Double)
      > Me.Name = Name
      > Me.Salary = Salary
      > End Sub
      >
      > Public Name As String
      > Public Salary As Double
      > End Class
      > ///
      >
      > Collections are unsorted, thus runtime is in O(n) if the collection
      > contains n items.
      >[/color]

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Find in Collection

        "Joerg Battermann" <jb@justbe.co m> schrieb:[color=blue]
        > thanks for the quick reply, however is it possible to define a name in
        > your example using the Predicate(Of Person)(Address Of GetFrank)?
        >
        > I am calling a function with an IDNumber as variable and it shall find me
        > the one Requirement that has that particular IDNumber... and return it or
        > do something with it[/color]

        I didn't have enough time to play around with this, but maybe the solution
        below will help you solve the problem (quick and dirty!):

        \\\
        Public Class Form1
        Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
        System.EventArg s) Handles MyBase.Load
        Dim x As New List(Of Person)
        x.Add(New Person("Peter", 1099))
        x.Add(New Person("Frank", 342234))
        x.Add(New Person("Fergus" , 23423))
        x.Add(New Person("Bill", 234))
        Dim p As Person = (New PersonFinder()) .Find(x, "Frank")
        MsgBox(p.Name)
        End Sub
        End Class

        Public Class PersonFinder
        Inherits Finder(Of Person)

        Protected Overrides Function InternalFind(By Val obj As Person) As
        Boolean
        Return obj.Name = DirectCast(Para meters(0), String)
        End Function
        End Class

        Public Class Person
        Public Sub New(ByVal Name As String, ByVal Salary As Double)
        Me.Name = Name
        Me.Salary = Salary
        End Sub

        Public Name As String
        Public Salary As Double
        End Class

        Public MustInherit Class Finder(Of T)
        Private m_Parameters() As Object

        Protected Property Parameters() As Object()
        Get
        Return m_Parameters
        End Get
        Private Set(ByVal Value As Object())
        m_Parameters = Value
        End Set
        End Property

        Public Function Find( _
        ByVal List As List(Of T), _
        ByVal ParamArray Parameters() As Object _
        ) As T
        Me.Parameters = Parameters
        Return List.Find(Addre ssOf InternalFind)
        End Function

        Protected Overridable Function InternalFind( _
        ByVal obj As T _
        ) As Boolean
        '
        End Function
        End Class
        ///

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Joerg Battermann

          #5
          Re: Find in Collection

          Thanks a lot!!


          Herfried K. Wagner [MVP] wrote:[color=blue]
          > "Joerg Battermann" <jb@justbe.co m> schrieb:[color=green]
          >> thanks for the quick reply, however is it possible to define a name in
          >> your example using the Predicate(Of Person)(Address Of GetFrank)?
          >>
          >> I am calling a function with an IDNumber as variable and it shall find
          >> me the one Requirement that has that particular IDNumber... and return
          >> it or do something with it[/color]
          >
          > I didn't have enough time to play around with this, but maybe the
          > solution below will help you solve the problem (quick and dirty!):
          >
          > \\\
          > Public Class Form1
          > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
          > System.EventArg s) Handles MyBase.Load
          > Dim x As New List(Of Person)
          > x.Add(New Person("Peter", 1099))
          > x.Add(New Person("Frank", 342234))
          > x.Add(New Person("Fergus" , 23423))
          > x.Add(New Person("Bill", 234))
          > Dim p As Person = (New PersonFinder()) .Find(x, "Frank")
          > MsgBox(p.Name)
          > End Sub
          > End Class
          >
          > Public Class PersonFinder
          > Inherits Finder(Of Person)
          >
          > Protected Overrides Function InternalFind(By Val obj As Person) As
          > Boolean
          > Return obj.Name = DirectCast(Para meters(0), String)
          > End Function
          > End Class
          >
          > Public Class Person
          > Public Sub New(ByVal Name As String, ByVal Salary As Double)
          > Me.Name = Name
          > Me.Salary = Salary
          > End Sub
          >
          > Public Name As String
          > Public Salary As Double
          > End Class
          >
          > Public MustInherit Class Finder(Of T)
          > Private m_Parameters() As Object
          >
          > Protected Property Parameters() As Object()
          > Get
          > Return m_Parameters
          > End Get
          > Private Set(ByVal Value As Object())
          > m_Parameters = Value
          > End Set
          > End Property
          >
          > Public Function Find( _
          > ByVal List As List(Of T), _
          > ByVal ParamArray Parameters() As Object _
          > ) As T
          > Me.Parameters = Parameters
          > Return List.Find(Addre ssOf InternalFind)
          > End Function
          >
          > Protected Overridable Function InternalFind( _
          > ByVal obj As T _
          > ) As Boolean
          > '
          > End Function
          > End Class
          > ///
          >[/color]

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Find in Collection

            Joerg,
            In addition to the other comments.

            I posted a sample Generic Find routine in April that uses Predicate(Of T, V)
            instead of Predicate(Of T). Predicate(Of T, V) allows you to pass the value
            you are looking for in addition to the object you are looking at.

            See these posts:





            Reviewing the entire thread might be helpful:



            --
            Hope this helps
            Jay [MVP - Outlook]
            ..NET Application Architect, Enthusiast, & Evangelist
            T.S. Bradley - http://www.tsbradley.net


            "Joerg Battermann" <jb@justbe.co m> wrote in message
            news:438f12f5$0 $20839$9b4e6d93 @newsread2.arco r-online.net...
            | Hello there,
            |
            | I have a custom type defined via
            |
            | [...]
            | Public Class Requirement
            | Public IDNumber As Integer
            | Public Name As String
            | Public Description As String
            | Public VersionPlanAttr ibutes As New _
            | System.Collecti ons.Generic.Lis t(Of Attribute)
            | Public Traces As New System.Collecti ons.Generic.Lis t(Of Trace)
            | Public Checked As Boolean = False
            | End Class
            | [...]
            |
            | and want to find one particular item (identified via IDNumber) in a
            | filled collection of these items declared via
            |
            | [...]
            | Public ERSRequirements As New System.Collecti ons.Generic.Lis t(Of_
            | Requirement)
            | [...]
            |
            |
            | .. is there any quick way to do it or would I have to loop through the
            | collection?
            |
            |
            | Best Regards and thanks,
            | -Joerg


            Comment

            Working...