Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Proper way to use .contains and .find methods with List(of object)

Proper way to use .contains and .find methods with List(of object)

Scheduled Pinned Locked Moved Visual Basic
tutorialquestioncsharpwinformsdebugging
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jon_Boy
    wrote on last edited by
    #1

    Could someone explain how to properly use the .contains and .find methods on generic list? For example, I created a list(of reportparameter) below and fill it with some dummy test values. I can't get either the .contains or find methods to work.

    Dim params As New List(Of Microsoft.Reporting.WinForms.ReportParameter)
    params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param1", "ParamValue1", False))
    params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param2", "ParamValue2", False))
    params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param3", "ParamValue3", False))

    'Compiles, but never returns true. Is it correct to create a new param to execute .contains?
    If params.Contains(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
    Debug.Print("")
    End If

    'This doesn't compile, not sure of the correct way to perform a find
    If params.Find(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
    Debug.Print("")
    End If

    Sorry if this is a dumb question - having a blonde moment today.

    "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

    M A 2 Replies Last reply
    0
    • J Jon_Boy

      Could someone explain how to properly use the .contains and .find methods on generic list? For example, I created a list(of reportparameter) below and fill it with some dummy test values. I can't get either the .contains or find methods to work.

      Dim params As New List(Of Microsoft.Reporting.WinForms.ReportParameter)
      params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param1", "ParamValue1", False))
      params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param2", "ParamValue2", False))
      params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param3", "ParamValue3", False))

      'Compiles, but never returns true. Is it correct to create a new param to execute .contains?
      If params.Contains(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
      Debug.Print("")
      End If

      'This doesn't compile, not sure of the correct way to perform a find
      If params.Find(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
      Debug.Print("")
      End If

      Sorry if this is a dumb question - having a blonde moment today.

      "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

      M Offline
      M Offline
      Manfred Rudolf Bihy
      wrote on last edited by
      #2

      Your problem is that you have to use the same object reference, but in your example you are creating two different objects. The way you are doing it can only work if the Equal method is so defined that it compares the components the class with one another. Best Regards, -MRB

      1 Reply Last reply
      0
      • J Jon_Boy

        Could someone explain how to properly use the .contains and .find methods on generic list? For example, I created a list(of reportparameter) below and fill it with some dummy test values. I can't get either the .contains or find methods to work.

        Dim params As New List(Of Microsoft.Reporting.WinForms.ReportParameter)
        params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param1", "ParamValue1", False))
        params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param2", "ParamValue2", False))
        params.Add(New Microsoft.Reporting.WinForms.ReportParameter("Param3", "ParamValue3", False))

        'Compiles, but never returns true. Is it correct to create a new param to execute .contains?
        If params.Contains(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
        Debug.Print("")
        End If

        'This doesn't compile, not sure of the correct way to perform a find
        If params.Find(New Microsoft.Reporting.WinForms.ReportParameter("Param1")) Then
        Debug.Print("")
        End If

        Sorry if this is a dumb question - having a blonde moment today.

        "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #3

        I don't think ReportParameter is IEquatable, so you must use your own predicate to find the item (see below code). Also, even if it were IEquatable, your code uses two different constructors (a 3-parameter constructor and a 1-parameter constructor), which would set different variables in the object, so they wouldn't be equal anyway.

        Dim params As New List(Of ReportParameter)
        params.Add(New ReportParameter("Param1", "Value1", False))
        Dim foundParam As ReportParameter =
        params.Find(New Predicate(Of ReportParameter)(Function(rp As ReportParameter) rp.Name = "Param1"))
        If foundParam Is Nothing Then
        Debug.Print("Doesn't contain.")
        Else
        Debug.Print("Does contain.")
        End If

        [

        S<T>::f(U) // Out of line.

        ](http://msdn.microsoft.com/en-us/library/8yk3t00s(v=vs.71).aspx)

        modified on Friday, May 6, 2011 11:30 AM

        J T 2 Replies Last reply
        0
        • A AspDotNetDev

          I don't think ReportParameter is IEquatable, so you must use your own predicate to find the item (see below code). Also, even if it were IEquatable, your code uses two different constructors (a 3-parameter constructor and a 1-parameter constructor), which would set different variables in the object, so they wouldn't be equal anyway.

          Dim params As New List(Of ReportParameter)
          params.Add(New ReportParameter("Param1", "Value1", False))
          Dim foundParam As ReportParameter =
          params.Find(New Predicate(Of ReportParameter)(Function(rp As ReportParameter) rp.Name = "Param1"))
          If foundParam Is Nothing Then
          Debug.Print("Doesn't contain.")
          Else
          Debug.Print("Does contain.")
          End If

          [

          S<T>::f(U) // Out of line.

          ](http://msdn.microsoft.com/en-us/library/8yk3t00s(v=vs.71).aspx)

          modified on Friday, May 6, 2011 11:30 AM

          J Offline
          J Offline
          Jon_Boy
          wrote on last edited by
          #4

          Outstanding. Thanks for the clear explanation and example.

          "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

          1 Reply Last reply
          0
          • A AspDotNetDev

            I don't think ReportParameter is IEquatable, so you must use your own predicate to find the item (see below code). Also, even if it were IEquatable, your code uses two different constructors (a 3-parameter constructor and a 1-parameter constructor), which would set different variables in the object, so they wouldn't be equal anyway.

            Dim params As New List(Of ReportParameter)
            params.Add(New ReportParameter("Param1", "Value1", False))
            Dim foundParam As ReportParameter =
            params.Find(New Predicate(Of ReportParameter)(Function(rp As ReportParameter) rp.Name = "Param1"))
            If foundParam Is Nothing Then
            Debug.Print("Doesn't contain.")
            Else
            Debug.Print("Does contain.")
            End If

            [

            S<T>::f(U) // Out of line.

            ](http://msdn.microsoft.com/en-us/library/8yk3t00s(v=vs.71).aspx)

            modified on Friday, May 6, 2011 11:30 AM

            T Offline
            T Offline
            Tarun K S
            wrote on last edited by
            #5

            Excellent answer. :thumbsup:

            The more anger towards the past you carry in your heart, the less capable you are of loving in the present. My Blog![^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups