none
How to find an element in a list RRS feed

  • Question

  • Hi;

    I have a list like this:

    List<Message> MMMM = new List<Message>();

    Message class is identified by this:

           public class Message
           {
               public int Sender_ID { set; get; }
               public string sender { set; get; }
               public string _Message { set; get; }
           }

    I added some into the list:

                MMMM.Add(new Message { sender = "Ali", Sender_ID = 1 });
                MMMM.Add(new Message { sender = "hamid", _Message = "bbbbbbbbbbbbb", Sender_ID = 2 });
                MMMM.Add(new Message { sender = "Ali", _Message = "hhhhhhhhhhhjfjfjfjfjfjfjfjhh", Sender_ID = 1 });

    The problem is, I'm going to find out that if a message exists in the list and then I need to remove that.

    How could I do that?

    thanks,


    Mahd!


    Friday, October 5, 2012 6:51 AM

Answers

  • Ok,

    this will only work if you use the same object. You need to use the Exists() method like:

            private void button1_Click(object sender, EventArgs e)
            {
                List<Message> MMMM = new List<Message>();
                
                MMMM.Add(new Message { sender = "Ali", Sender_ID = 1 });
                MMMM.Add(new Message { sender = "hamid", _Message = "bbbbbbbbbbbbb", Sender_ID = 2 });
                MMMM.Add(new Message { sender = "Ali", _Message = "hhhhhhhhhhhjfjfjfjfjfjfjfjhh", Sender_ID = 1 });
                
                
                messageToFind = new Message { sender = "Ali", Sender_ID = 1 };
                bool j = MMMM.Exists(MessageExist);
               
            }
            private Message messageToFind;
            private Boolean MessageExist(Message m)
            {
                return (m._Message == messageToFind._Message) && (m.sender == messageToFind.sender) && (m.Sender_ID == messageToFind.Sender_ID);
            }


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Friday, October 5, 2012 7:31 AM
  • Also, I've found an other solution :

    http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx


    Mahd!

    Friday, October 5, 2012 7:45 AM

All replies

  • You can use the Contains() method. If it returns true you can use the Remove() method to remove the element.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Friday, October 5, 2012 6:58 AM
  • it's not working!

    I try this:

    bool j=  MMMM.Contains(new Message {sender="Ali", Sender_ID = 1 });
     

    returned False!!


    Mahd!

    Friday, October 5, 2012 7:18 AM
  • Ok,

    this will only work if you use the same object. You need to use the Exists() method like:

            private void button1_Click(object sender, EventArgs e)
            {
                List<Message> MMMM = new List<Message>();
                
                MMMM.Add(new Message { sender = "Ali", Sender_ID = 1 });
                MMMM.Add(new Message { sender = "hamid", _Message = "bbbbbbbbbbbbb", Sender_ID = 2 });
                MMMM.Add(new Message { sender = "Ali", _Message = "hhhhhhhhhhhjfjfjfjfjfjfjfjhh", Sender_ID = 1 });
                
                
                messageToFind = new Message { sender = "Ali", Sender_ID = 1 };
                bool j = MMMM.Exists(MessageExist);
               
            }
            private Message messageToFind;
            private Boolean MessageExist(Message m)
            {
                return (m._Message == messageToFind._Message) && (m.sender == messageToFind.sender) && (m.Sender_ID == messageToFind.Sender_ID);
            }


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Friday, October 5, 2012 7:31 AM
  • Also, I've found an other solution :

    http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx


    Mahd!

    Friday, October 5, 2012 7:45 AM