• 11
• 9
• 10
• 9
• 11

# C# List<T>.Find

This topic is 2042 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;