Results 1 to 5 of 5

Thread: [RESOLVED] [2005] ListBox.Contains() does not work for custom Classes

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Resolved [RESOLVED] [2005] ListBox.Contains() does not work for custom Classes

    Hi there,

    I have a Class called Server which has a toString() method and some other attributes. When I add instances of this class to the ListBox, the toString() function is called and it is displayed properly in the ListBox. However, I currently have no way to check if an object with identical attributes is already in this ListBox.
    I am sure there is a trick to this that involves overloading an operator of some kind. Could someone please tell me which one I need to implement in my Server class to make this work?

    Thanks in advance!
    Last edited by mightor; Apr 25th, 2007 at 02:35 AM. Reason: Forgot the version of vb.net in the title

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] ListBox.Contains() does not work for custom Classes

    Here's a chance to learn a good lesson. If you want to do something with or to a class the first thing you should do is go to the MSDN documentation for that class and its member listing. Doing that for the ListBox class would lead you to the FindString and FindStringExact methods. They will return ListBox.NoMatches if no matching value is found.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] ListBox.Contains() does not work for custom Classes

    Thank you. I will check it out. I am not trying to be lazy or anything. I think my main problem is that I tend to over-analyse the problem as things tend to be a lot more complicated to implement in languages other than vb.NET.
    MSDN is indeed my best friend, I just tend to get lost in it sometimes
    I will RTFMSDN some more, then

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] ListBox.Contains() does not work for custom Classes

    MSDN won't always give you what you need but in my experience it will more often than not. A lot of people use the search facility and think the sit eis rubbish because they can't find what they need. A lot of the time their key words are the problem, which may or not be the case for you. What I always say, though, is that if you know what class you're using then read about that class first and at least half the time you'll get your answer without having to search at all. It's worked for me since I was a rank beginner in .NET.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] ListBox.Contains() does not work for custom Classes

    Teach a man to fish and all that stuff
    Well I have figured it out and learned new stuff in the process so thanks for pointing me in the right direction. I have implemented an override for Equals() and that seems to have done the trick.

    I tried to add some rep to you but alas, the forum won't let me since I've already done that too many times in the past few days, sorry about that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width