C# List<T>.Find

Started by
33 comments, last by Anthracis 11 years, 7 months ago
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?
Advertisement
this line is wrong:

List<EngineObject> objectList = m_objects[typeof(ObjectInstance);

try adding a "]" behind the typeOf(ObjectInstance)

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion


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.
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>.
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..
Wild guess, but do you have the required using statements?


using System.Collections.Generic;
using System.Linq;
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.

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);
}

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;

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.

This topic is closed to new replies.

Advertisement