[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!
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.
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 :)
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. :)
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.