• Create Account

C# List<T>.Find

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

34 replies to this topic

#1Orangeatang  Members

2666
Like
0Likes
Like

Posted 17 September 2012 - 06:16 AM

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, 17 September 2012 - 06:56 AM.

#2NightCreature83  Members

4784
Like
1Likes
Like

Posted 17 September 2012 - 06:45 AM

this line is wrong:
List<EngineObject> objectList = m_objects[typeof(ObjectInstance);

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

Edited by NightCreature83, 17 September 2012 - 06:45 AM.

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

#3Orangeatang  Members

2666
Like
0Likes
Like

Posted 17 September 2012 - 06:55 AM

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.

#4Oberon_Command  Members

5385
Like
1Likes
Like

Posted 17 September 2012 - 10:05 AM

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>.

#5CyberRascal  Members

208
Like
1Likes
Like

Posted 17 September 2012 - 10:27 AM

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..

#6laztrezort  Members

1058
Like
1Likes
Like

Posted 17 September 2012 - 10:56 AM

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

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


#7Arthur Lotuz  Members

1596
Like
1Likes
Like

Posted 17 September 2012 - 12:00 PM

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.

#8Orangeatang  Members

2666
Like
0Likes
Like

Posted 17 September 2012 - 02:06 PM

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


#9Orangeatang  Members

2666
Like
0Likes
Like

Posted 17 September 2012 - 02:07 PM

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;


#10Orangeatang  Members

2666
Like
0Likes
Like

Posted 17 September 2012 - 02:08 PM

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.

#11Orangeatang  Members

2666
Like
0Likes
Like

Posted 17 September 2012 - 02:13 PM

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

#12Orangeatang  Members

2666
Like
0Likes
Like

Posted 17 September 2012 - 02:20 PM

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[i].ID == objectId )
{
return (T)((object)objectList[i]);
}
}
*/
}
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, 17 September 2012 - 02:21 PM.

#13lwm  Members

2358
Like
1Likes
Like

Posted 17 September 2012 - 03:07 PM

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;


current project: Roa

#14Arthur Lotuz  Members

1596
Like
1Likes
Like

Posted 17 September 2012 - 03:08 PM

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

This also does not work?

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;


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, 17 September 2012 - 03:10 PM.

#15Orangeatang  Members

2666
Like
0Likes
Like

Posted 17 September 2012 - 11:52 PM

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


#16Orangeatang  Members

2666
Like
0Likes
Like

Posted 18 September 2012 - 01:41 AM

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!

#17Mizu  Members

1374
Like
0Likes
Like

Posted 18 September 2012 - 05:04 AM

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

#18jefferytitan  Members

2508
Like
1Likes
Like

Posted 18 September 2012 - 05:34 AM

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.

#19CyberRascal  Members

208
Like
1Likes
Like

Posted 18 September 2012 - 06:13 AM

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?

#20Orangeatang  Members

2666
Like
0Likes
Like

Posted 18 September 2012 - 07:03 AM

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, 18 September 2012 - 07:04 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.