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. C#
  4. Contains method doesn't work on BindingList<t></t> [modified]

Contains method doesn't work on BindingList<t></t> [modified]

Scheduled Pinned Locked Moved C#
question
7 Posts 3 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.
  • M Offline
    M Offline
    mahraja
    wrote on last edited by
    #1

    Hi i have a BindingList and i want to check if the list contains an object of ListItem. But the Contains method of the BindingList<> always returns false. can anyone tell me why? and what should i do?

    BindingList<listItem> normalExtItems = new BindingList<listItem>();

    //elements are added to the bindingList here

    extItem = new listItem(Number,Name,Id));

    if(normalExtItems.Contains(extItem))//always returns false
    {
    //do sth
    }

    modified on Sunday, November 30, 2008 5:54 PM

    L W 2 Replies Last reply
    0
    • M mahraja

      Hi i have a BindingList and i want to check if the list contains an object of ListItem. But the Contains method of the BindingList<> always returns false. can anyone tell me why? and what should i do?

      BindingList<listItem> normalExtItems = new BindingList<listItem>();

      //elements are added to the bindingList here

      extItem = new listItem(Number,Name,Id));

      if(normalExtItems.Contains(extItem))//always returns false
      {
      //do sth
      }

      modified on Sunday, November 30, 2008 5:54 PM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I don't see any line of code adding something to normalExtItems, so it seems obvious Contains() returns false no matter what. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Fixturized forever. :confused:


      1 Reply Last reply
      0
      • M mahraja

        Hi i have a BindingList and i want to check if the list contains an object of ListItem. But the Contains method of the BindingList<> always returns false. can anyone tell me why? and what should i do?

        BindingList<listItem> normalExtItems = new BindingList<listItem>();

        //elements are added to the bindingList here

        extItem = new listItem(Number,Name,Id));

        if(normalExtItems.Contains(extItem))//always returns false
        {
        //do sth
        }

        modified on Sunday, November 30, 2008 5:54 PM

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        Since this is obviously partial code, do you insert extItem into the bindinglist somewhere. The code you posted cannot find extItem in the list because it's not added to it.

        The need to optimize rises from a bad design.My articles[^]

        M 1 Reply Last reply
        0
        • W Wendelius

          Since this is obviously partial code, do you insert extItem into the bindinglist somewhere. The code you posted cannot find extItem in the list because it's not added to it.

          The need to optimize rises from a bad design.My articles[^]

          M Offline
          M Offline
          mahraja
          wrote on last edited by
          #4

          yes, sure bindinglist has ListItems. Because it was in a long loop, i didn't add it to the code here. sorry it was not clear

          W 1 Reply Last reply
          0
          • M mahraja

            yes, sure bindinglist has ListItems. Because it was in a long loop, i didn't add it to the code here. sorry it was not clear

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            Just a simple test case. Didn't know what your listitem type was, but it doesn't matter. The following works as expected:

            System.Web.UI.WebControls.ListItem extItem;
            System.ComponentModel.BindingList<System.Web.UI.WebControls.ListItem> normalExtItems
            = new System.ComponentModel.BindingList<System.Web.UI.WebControls.ListItem>();
            extItem = new System.Web.UI.WebControls.ListItem("A","1", true);
            normalExtItems.Add(extItem);
            if(normalExtItems.Contains(extItem)) {
            //do sth
            }

            Have you checked (using debugger) that the list actually contains the item you're interested in.

            The need to optimize rises from a bad design.My articles[^]

            M 1 Reply Last reply
            0
            • W Wendelius

              Just a simple test case. Didn't know what your listitem type was, but it doesn't matter. The following works as expected:

              System.Web.UI.WebControls.ListItem extItem;
              System.ComponentModel.BindingList<System.Web.UI.WebControls.ListItem> normalExtItems
              = new System.ComponentModel.BindingList<System.Web.UI.WebControls.ListItem>();
              extItem = new System.Web.UI.WebControls.ListItem("A","1", true);
              normalExtItems.Add(extItem);
              if(normalExtItems.Contains(extItem)) {
              //do sth
              }

              Have you checked (using debugger) that the list actually contains the item you're interested in.

              The need to optimize rises from a bad design.My articles[^]

              M Offline
              M Offline
              mahraja
              wrote on last edited by
              #6

              yes, no matter the list has the item or not, it always returns false, i have checked that so many times :(

              W 1 Reply Last reply
              0
              • M mahraja

                yes, no matter the list has the item or not, it always returns false, i have checked that so many times :(

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #7

                Did the test case work for you? Also the Contains method checks if the actual object is present in the list so if you create a similar object (all the properties are the same) it won't find it since it's not the same object.

                The need to optimize rises from a bad design.My articles[^]

                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