Sign in to follow this  
Capoeirista

C# List<T>.Find

Recommended Posts

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

[CODE]
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;
}
[/CODE]

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
[quote name='NightCreature83' timestamp='1347885909' post='4980857']
this line is wrong:
try adding a "]" behind the typeOf(ObjectInstance)
[/quote]
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
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
[quote name='CyberRascal' timestamp='1347899238' post='4980929']
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..
[/quote]
Yeah seems like it should to me too. What's the LINQ extension?

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

[CODE]
/// <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[i].ID == objectId )
{
return (T)((object)objectList[i]);
}
}*/
}
return default(T);
}
[/CODE]

Share this post


Link to post
Share on other sites
[quote name='laztrezort' timestamp='1347900977' post='4980942']
Wild guess, but do you have the required using statements?

[code]
using System.Collections.Generic;
using System.Linq;
[/code]
[/quote]

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

[CODE]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
[/CODE]

Share this post


Link to post
Share on other sites
[quote name='Arthur Souza' timestamp='1347904825' post='4980965']
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.
[/quote]
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
[quote name='Oberon_Command' timestamp='1347897952' post='4980922']
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>.
[/quote]
Tried adding a method to my class to use for searching, but I get the exact same error back

Share this post


Link to post
Share on other sites
[quote name='CyberRascal' timestamp='1347899238' post='4980929']
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..
[/quote]
I looked up how to use LINQ, and the following does work :
[CODE]
/// <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[i].ID == objectId )
{
return (T)((object)objectList[i]);
}
}
*/
}
return default(T);
}
[/CODE]

There are obviously a couple of alternate approaches I can use now, but this Find problem has become something of an academic question [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Capoeirista

Share this post


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

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?

[CODE]
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;
[/CODE]

Share this post


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

This also does not work?

[quote name='lwm' timestamp='1347916064' post='4981022']
[quote name='Capoeirista' timestamp='1347913255' post='4981007']
I looked up how to use LINQ, and the following does work :
[/quote]
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?
[CODE]
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;
[/CODE]
[/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 this post


Link to post
Share on other sites
[quote name='Arthur Souza' timestamp='1347916102' post='4981023']
objectList.Find( delegate(EngineObject obj) { return obj.ID == objectId; } );

This also does not work?
[/quote]
No that doesn't work - I get the same compile error as :

[CODE]
objectList.Find( i => i.ID == objectId );
[/CODE]

Share this post


Link to post
Share on other sites
[quote name='lwm' timestamp='1347916064' post='4981022']
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?
[/quote]
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 this post


Link to post
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


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


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


Link to post
Share on other sites
[quote name='CyberRascal' timestamp='1347970380' post='4981221']
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?
[/quote]
This just gets stranger :

[CODE]
EngineObject elObjectO = objectList.First( x => x.ID == objectId );
[/CODE]

Works perfectly, as does :

[CODE]
EngineObject elObjectO = objectList.FirstOrDefault( x => x.ID == objectId );
[/CODE]

Just not :

[CODE]
EngineObject elObjectO = objectList.Find( x => x.ID == objectId );
[/CODE]

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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Capoeirista

Share this post


Link to post
Share on other sites
[quote name='Mizu' timestamp='1347966259' post='4981201']
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]
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 this post


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


Link to post
Share on other sites
[quote name='Xai' timestamp='1347975954' post='4981246']
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.
[/quote]
Thanks for the advice - I had a look at the Predicate that the Find operation, and sure enough it's

[CODE]
Predicate<EngineObject> match
[/CODE]

I'll keep looking

Share this post


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


Link to post
Share on other sites
[quote name='Mizu' timestamp='1347981027' post='4981273']
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.
[/quote]

Just tried this out :

[CODE]
Predicate<EngineObject> pred = i => i.ID == objectId;
objectList.Find( pred );
[/CODE]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this