 |
| C# 2005 For discussion of Visual C# 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 12th, 2009, 11:03 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
When to use delegates or how to add a generic find method.
Hello...
I'm facing a dilemma here, and can't find enough info on the web (probably asking poor questions to google).
The thing is, I have a list<T> where I want to find items. I'm already wrote a delegate in two places to find items inside there (one to find by id, one to find by name). The thing is that I probably missing something somewhere, because I don't want to write the same delegate every time I need to find an item, so I want to  override  the find method (or at least write somewhere the definition of the find) so I can just call it without the delegate.
Is this posible? I remember that in java you can overwrite the compareto method to do something like that.
I'm doing something wrong? I have to write a delegate everywhere? or I just need to inherit something to override the method and avoid the delegate??
Thanks for reading!
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========
|
|

March 16th, 2009, 04:38 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Ummm...why not create your own ExtendedList<T> class that adds those methods??
If List<T> isn't inheritable, just contain it in your class. If it is, inherit it.
Am I missing something, a reason you feel a need to use delegates instead of just extending the class??
|
|

March 16th, 2009, 04:42 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Oh, wait...are you wanting to add find methods that are specific to a particular class T in the List<T>??
Is that what you mean?
Still think you might want to do it by your own ExtendedList<ExtendedT>. That is, you specify that the ExtendedT must implement an interface that you design that is used to implement the kinds of Find that you want to do. Now your own Find can of course rely on that interface existing for the given element type.
Hmmm???
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
|
|

March 16th, 2009, 06:28 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
|
|
Generic List Find methods
Are you saying you want to create a generic Find method which lets you filter by any property in the object type? I had the same issue a few years back and created a couple of methods you can put in an inherited List<T> (List<T> is inheritable but the Find method is not overridable).
My version actually allows filtering by multiple properties. It's not the most efficient code, especially the criteria part, but has done me well enough.
Code:
/// <summary>Searches the list for matching entries</summary>
/// <param name="criteria">A list of KeyValuePairs specifying property names and values to match</param>
/// <returns>All instances in the list matching all the criteria</returns>
public List<T> Search(IEnumerable<KeyValuePair<String, Object>> criteria)
{
// put into a list so we can use List methods
List<KeyValuePair<String, Object>> criteriaList = new List<KeyValuePair<string, object>>(criteria);
// set up variables
Type objecttype = typeof(T);
PropertyInfo prop;
object propvalue;
// run delegate to find all matching entities
return new List<T>(this.FindAll(delegate(T obj)
{
// check each criteria value with the current entity
// returns true if all properties match their corresponding required value
return criteriaList.TrueForAll(delegate(KeyValuePair<string, object> kvp)
{
// read stuff from property
object objtomatch = kvp.Value;
prop = objecttype.GetProperty(kvp.Key);
propvalue = prop.GetValue(obj, null);
// compare values
if (objtomatch == null)
return (propvalue == null);
return objtomatch.Equals(propvalue);
});
}));
}
/// <summary>Searches the list for matching entries</summary>
/// <param name="matchProperty">The name of the property to match</param>
/// <param name="requiredValue">The value to match</param>
/// <returns>All instances in the list matching the criteria</returns>
public List<T> Search(string matchProperty, object requiredValue)
{
return this.Search(new KeyValuePair<String, Object>[] {
new KeyValuePair<String,Object>(matchProperty, requiredValue)
});
}
You can then call it for a single property as so:
Code:
List<Person> results = bigList.Search("Name", "Phil");
There is also a similar idea for searching ILists at http://www.paraesthesia.com/archive/...filtering.aspx
HTH
Phil
|
|
The Following User Says Thank You to philip_cole For This Useful Post:
|
|
|

March 16th, 2009, 08:05 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Thanks old pedant and phil. From your code phil I get a great Idea. The thing is: this program supossed to be MVC full compliant(?), anyway, it has several list containing all the objects that are currently loaded. Right now, when it needs a new object (that are related to others), it just load it and all it's dependencies. So I have several times the same object loaded everywhere.
I'm fixing that, but for that I need to find in every list the object before trying to load a new one. That means I'm doing this:
Code:
object o = list.find(delegate (object o){return (o.id == currentid);});
I wanted to avoid that, in every part of the code. Just send the correct function to the find method. But since I have several list of distinct type of object, I didn't know where to put the code.
Your idea phil get me another idea that I will try later.
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========
|
|
 |