Jump to content
  • Advertisement
Sign in to follow this  
Capoeirista

C# List<T>.Find

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
this line is wrong:

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

try adding a "]" behind the typeOf(ObjectInstance) Edited by NightCreature83

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!