Solved

C#: List contains custom object

Posted on 2008-10-01
5
13,779 Views
Last Modified: 2013-12-17
Is there any way of using System.Collections.List.Contains() method to check if a collection of specific custom object has a given object, given that only one or few property (ies) of the object to look for are taken into account?
0
Comment
Question by:karakav
5 Comments
 
LVL 8

Expert Comment

by:rambovn
ID: 22613152
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22613201
use System.Collections.List.Find()  to implement custom search
try this sample...
public class TestObject

{

	public TestObject()

	{
 

		System.Collections.Generic.List<testCalss> lstTestClass = new List<testCalss>();
 

		lstTestClass.Add(new testCalss(10 , 20));

		lstTestClass.Add(new testCalss(10 , 20));

		lstTestClass.Add(new testCalss(15 , 20));
 

		testCalss ObjClassFound;

		ObjClassFound = lstTestClass.Find(findObject);
 

	}
 
 

	public bool findObject(testCalss val)

	{

		if (val.iNumber1 == 15)

			return true;

		else

			return false;

	}
 

}

public class testCalss

{

	public Int32 iNumber1;

	public Int32 iNumber2;

	public testCalss(Int32 _iNumber1,Int32 _iNumber2)

	{

		this.iNumber1 = _iNumber1;

		this.iNumber2 = _iNumber2;

	}

}

Open in new window

0
 
LVL 4

Author Comment

by:karakav
ID: 22613326
What if I want to pass the value to compare to in the findObject delegate?
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 22615177
use an anonymous delegate or create a small wrapper class for your delegate.

There are plenty of examples of using anonymous delegates available on the web and the latter basically does the same thing by hand that the compiler does with an anonymous delegate.

class MyTestClassMatcher {
   private readonly int number1;
   private readonly int number2;

   public MyTestClassMatcher(int _number1, int _number2) {
       number1 = _number1;
       number2 = _number2;
   }

   public bool IsMatch(testCalss obj) {
         return obj.Number1 == number1 && obj.Number2 == number2;
   }
}

To use like in the example above

                lstTestClass.Add(new testCalss(10 , 20));
                lstTestClass.Add(new testCalss(10 , 20));
                lstTestClass.Add(new testCalss(15 , 20));
 
                MyClassFinder finder = new MyClassFinder(10, 20);
                ObjClassFound = lstTestClass.Find(finder.IsMatch);

to read more about how to use anonymous delegates for this check out http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx


but basically it would be ...

public testClass SearchList(List<testClass>list, int value1, int value2) {
    return list.Find(delegate(testClass obj)
                             {
                                   return obj.number1 == value1 && obj.number2 == value2;
                             }
                          );
}

Cheers,

Greg
0
 
LVL 4

Author Closing Comment

by:karakav
ID: 31501912
Thanks a lot. This tip is really amazing.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now