# C# List<T>.Find

This topic is 1953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey folks,
Reasonably new to C# here, and was wondering if you could point out what I'm doing wrong here :

 class ObjectManager { public ObjectInstance GetObject<ObjectInstance>( int objectId ) { if( m_objects.ContainsKey(typeof(ObjectInsance) ) { List<EngineObject> objectList = m_objects[typeof(ObjectInstance)]; // This doesn't compile EngineObject obj = objectList.Find( i => i.ID == objectId ); } } ... Dictionary<Type, List<EngineObject>> m_objects; } 

I'm trying to return an EngineObject instance given an ID. EngineObject contains the ID property, but compiling generates the following error :

Error 2 'System.Collections.Generic.List<Engine.Objects.EngineObject>' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Collections.Generic.List<Engine.Objects.EngineObject>' could be found (are you missing a using directive or an assembly reference?) C:\Code\Outskirts\Engine\Managers\ObjectManager.cs 64

Any ideas what I'm doing wrong here? Edited by Capoeirista

##### Share on other sites
this line is wrong:
 List<EngineObject> objectList = m_objects[typeof(ObjectInstance); 
try adding a "]" behind the typeOf(ObjectInstance) Edited by NightCreature83

##### Share on other sites

this line is wrong:
try adding a "]" behind the typeOf(ObjectInstance)

That was just a typo for this post

I can get the List<EngineObject> instance without problems - and can iterate through it to find the object with the supplied ID... just can't seem to do it using a delegate for the Find operation. EngineObject is abstract, but changing that to a standard class definition didn't work either.

##### Share on other sites
Does it work if you extract the lambda you're passing into Find() into a separate function? The error message makes it seem like it's complaining that you're passing it a List<EngineObject> instead of a Predicate<EngineObject>.

##### Share on other sites
Seems to me like that should compile. Try using the LINQ extension First instead (just for kicks). Also, do you have some variable named i in scope? Neither should give you the error you are getting though... I can't think of anything that could make the lambda infer as a List..

##### Share on other sites
Wild guess, but do you have the required using statements?

 using System.Collections.Generic; using System.Linq; 

##### Share on other sites
Is that the only error you are getting while compiling? I see its "Error 2" there, there doesnt seem to be anything wrong with that piece of code, even tho it doesnt compile and show an error there, it can be just a consequence of an error somewhere else.

##### Share on other sites

Seems to me like that should compile. Try using the LINQ extension First instead (just for kicks). Also, do you have some variable named i in scope? Neither should give you the error you are getting though... I can't think of anything that could make the lambda infer as a List..

Yeah seems like it should to me too. What's the LINQ extension?

No other variables in scope... this is the complete function :

 /// <summary> /// Returns an Object of the supplied type, given it's id /// </summary> public T GetObject<T>( int objectId ) { if( m_objectInstances.ContainsKey(typeof(T)) ) { List<EngineObject> objectList = m_objectInstances[typeof(T)]; objectList.Find( delegate(EngineObject obj) { return obj.ID == objectId; } ); /* for( int i = 0; i < objectList.Count; i++ ) { if( objectList.ID == objectId ) { return (T)((object)objectList); } }*/ } return default(T); } 

##### Share on other sites

Wild guess, but do you have the required using statements?

 using System.Collections.Generic; using System.Linq; 

Thanks for the idea - I had Collections.Generic, but not System.Linq... doesn't seem to make any difference though :

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; 

##### Share on other sites

Is that the only error you are getting while compiling? I see its "Error 2" there, there doesnt seem to be anything wrong with that piece of code, even tho it doesnt compile and show an error there, it can be just a consequence of an error somewhere else.

Yeah that's the only error there is now. I think the second error was the same as the first because it came up twice... the code is in a project that builds to a static library, so one error for the lib, and one for the other project that uses the lib.

##### Share on other sites

Does it work if you extract the lambda you're passing into Find() into a separate function? The error message makes it seem like it's complaining that you're passing it a List<EngineObject> instead of a Predicate<EngineObject>.

Tried adding a method to my class to use for searching, but I get the exact same error back

##### Share on other sites

Seems to me like that should compile. Try using the LINQ extension First instead (just for kicks). Also, do you have some variable named i in scope? Neither should give you the error you are getting though... I can't think of anything that could make the lambda infer as a List..

I looked up how to use LINQ, and the following does work :
 /// <summary> /// Returns an Object of the supplied type, given it's id /// </summary> public T GetObject<T>( int objectId ) { if( m_objectInstances.ContainsKey(typeof(T)) ) { List<EngineObject> objectList = m_objectInstances[typeof(T)]; var result = from obj in objectList where obj.ID == objectId select obj; /* for( int i = 0; i < objectList.Count; i++ ) { if( objectList.ID == objectId ) { return (T)((object)objectList); } } */ } return default(T); } 

There are obviously a couple of alternate approaches I can use now, but this Find problem has become something of an academic question Edited by Capoeirista

##### Share on other sites

I looked up how to use LINQ, and the following does work :

That method will always return default(T).

But as an aside, if you're going to look up an object by its id, why not use a dictionary for the collection of EngineObjects as well?

 public T GetObject<T>(int objectId) where T : EngineObject { Dictionary<int, EngineObject> objects; if (m_objectInstances.TryGetValue(typeof(T), out objects)) return (T)objects[objectId]; else return default(T); } private Dictionary<Type, Dictionary<int, EngineObject>> m_objectInstances; 

##### Share on other sites
objectList.Find( delegate(EngineObject obj) { return obj.ID == objectId; } );

This also does not work?

[quote name='Capoeirista' timestamp='1347913255' post='4981007']
I looked up how to use LINQ, and the following does work :

That method will always return default(T).
But as an aside, if you're going to look up an object by its id, why not use a dictionary for the collection of EngineObjects as well?
 public T GetObject<T>(int objectId) where T : EngineObject { Dictionary<int, EngineObject> objects; if (m_objectInstances.TryGetValue(typeof(T), out objects)) return (T)objects[objectId]; else return default(T); } private Dictionary<Type, Dictionary<int, EngineObject>> m_objectInstances; 
[/quote]
I think he knows that will only return Default(T), as he commented the section that was actually going to do something to test the part of the code where he's having issues. Edited by Arthur Souza

##### Share on other sites

objectList.Find( delegate(EngineObject obj) { return obj.ID == objectId; } );

This also does not work?

No that doesn't work - I get the same compile error as :

 objectList.Find( i => i.ID == objectId ); 

##### Share on other sites

That method will always return default(T).

But as an aside, if you're going to look up an object by its id, why not use a dictionary for the collection of EngineObjects as well?

Hah hah, yeah - I was just trying to test out the compiling... that one won't make it through to a commit

I could use a dictionary for the object ids as well, but to start with I'd like to figure out why the Find operation is being so problematic.

Cheers!

##### Share on other sites
Soo... I tried pasting the code you have supplied in a new XNA project in Visual Studio 2010. Compiles fine. Then I did the same in VS2008. No errors there either. It seems like the errors is somewhere else in the code. What compiler are you using? If you don't mind, maybe you can zip the project and attach it to a post here so we can take a look

##### Share on other sites
I'm glad you found a working solution. If you want to use the Linq extension methods on collections you need to have the "using System.Linq;" line. It got me a bunch of times because it doesn't give you very useful compile errors. Personally I'm a fan of FirstOrDefault<T>, it allows a predicate and also works for empty collections.

##### Share on other sites
Yes, what I meant with the "First" LINQ extension was actually the method CALLED First:

var y = myList.First(x => x.ID == id);

or

var y = myList.FirstOrDefault(x => x.ID == id);

This is getting weird... Have you done a clean solution and then a rebuild solution?

##### Share on other sites

Yes, what I meant with the "First" LINQ extension was actually the method CALLED First:

var y = myList.First(x => x.ID == id);

or

var y = myList.FirstOrDefault(x => x.ID == id);

This is getting weird... Have you done a clean solution and then a rebuild solution?

This just gets stranger :

 EngineObject elObjectO = objectList.First( x => x.ID == objectId ); 

Works perfectly, as does :

 EngineObject elObjectO = objectList.FirstOrDefault( x => x.ID == objectId ); 

Just not :

 EngineObject elObjectO = objectList.Find( x => x.ID == objectId ); 

I thought it might be something to do with EngineObject being abstract... but no.

And yeah, I've cleaned my solution and rebuilt... I also reboot my PC Edited by Capoeirista

##### Share on other sites

Soo... I tried pasting the code you have supplied in a new XNA project in Visual Studio 2010. Compiles fine. Then I did the same in VS2008. No errors there either. It seems like the errors is somewhere else in the code. What compiler are you using? If you don't mind, maybe you can zip the project and attach it to a post here so we can take a look

I'd be happy to link to upload my engine project, but don't have a premium membership. I'm using VS2010. Edited by Capoeirista

##### Share on other sites
The "Find" function is a member of the List<T> class, the other functions are LINQ statements. These have different signatures (Find takes a Predicate<T>, where the LINQ functions take Func<T, bool>) and are implemented in completely different libraries / namespaces. This must be the root of the problem, but I don't have a compiler handy to investigate. When in doubt and using LINQ, hover over the function to see which signature it is trying to use and then find examples to match that (for instance look to an OLDER, .NET 2.0 example for using Find before LINQ instead of a newer 3.5 or 4.0 example).

I'm also not sure if the problem will be in some assumption the Predicate version makes about your class "EngineObject" or if it will be a simple issue with the syntax having to be slightly different. Good Luck.

##### Share on other sites

The "Find" function is a member of the List<T> class, the other functions are LINQ statements. These have different signatures (Find takes a Predicate<T>, where the LINQ functions take Func<T, bool>) and are implemented in completely different libraries / namespaces. This must be the root of the problem, but I don't have a compiler handy to investigate. When in doubt and using LINQ, hover over the function to see which signature it is trying to use and then find examples to match that (for instance look to an OLDER, .NET 2.0 example for using Find before LINQ instead of a newer 3.5 or 4.0 example).

I'm also not sure if the problem will be in some assumption the Predicate version makes about your class "EngineObject" or if it will be a simple issue with the syntax having to be slightly different. Good Luck.

Thanks for the advice - I had a look at the Predicate that the Find operation, and sure enough it's

 Predicate<EngineObject> match 

I'll keep looking

##### Share on other sites
Oops, sorry.. didn't know you had to have premium membership to do that

Anyway... Here's a random wild guess: what happens if you write it like this:
[source lang="csharp"]Predicate<EngineObject> pred = i => i.ID == objectId;

EngineObject obj = objectList.Find( pred );[/source]
Is the errors still with the Find() function, or is it with the predicate?

This surely is weird.. My last advice is to create a new project, isolate your ObjectManager and EngineObject, copy those to the new project and try compiling. If it works, add stuff until it stops working. If it still doesn't work it should be small enough to post the code in code tags here.

##### Share on other sites

This surely is weird.. My last advice is to create a new project, isolate your ObjectManager and EngineObject, copy those to the new project and try compiling. If it works, add stuff until it stops working. If it still doesn't work it should be small enough to post the code in code tags here.

Just tried this out :

 Predicate<EngineObject> pred = i => i.ID == objectId; objectList.Find( pred ); 

aaaand the same error code... this is getting ridiculous