Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


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.

  • You cannot reply to this topic
34 replies to this topic

#21 Orangeatang   Members   -  Reputation: 1782

Like
0Likes
Like

Posted 18 September 2012 - 07:10 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 Posted Image

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


Sponsor:

#22 Xai   Crossbones+   -  Reputation: 1459

Like
1Likes
Like

Posted 18 September 2012 - 07:45 AM

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.

#23 Orangeatang   Members   -  Reputation: 1782

Like
0Likes
Like

Posted 18 September 2012 - 08:00 AM

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.

Thanks for the advice - I had a look at the Predicate that the Find operation, and sure enough it's

Predicate<EngineObject> match

I'll keep looking

#24 Mizu   Members   -  Reputation: 1080

Like
1Likes
Like

Posted 18 September 2012 - 09:10 AM

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.

#25 Orangeatang   Members   -  Reputation: 1782

Like
0Likes
Like

Posted 18 September 2012 - 09:43 AM

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.


Just tried this out :

Predicate<EngineObject> pred = i => i.ID == objectId;
objectList.Find( pred );

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

#26 CyberRascal   Members   -  Reputation: 208

Like
1Likes
Like

Posted 18 September 2012 - 09:58 AM

Why don't you rar it and use say http://www.speedyshare.com/ to host it so we can check it out?

Edited by CyberRascal, 18 September 2012 - 10:00 AM.


#27 Orangeatang   Members   -  Reputation: 1782

Like
0Likes
Like

Posted 18 September 2012 - 10:23 AM

Why don't you rar it and use say http://www.speedyshare.com/ to host it so we can check it out?


Done http://www.2shared.com/file/GWlPZyxm/Engine.html

Edited by Capoeirista, 18 September 2012 - 03:35 PM.


#28 Arthur Souza   Members   -  Reputation: 1419

Like
1Likes
Like

Posted 18 September 2012 - 12:19 PM

Could you link me the other stuff you have reference on the project? Like JigLibX

#Woops, nevermind, downloading already. Gonna take a look at it right now.

Edited by Arthur Souza, 18 September 2012 - 12:20 PM.

A.

Lotus - Action RPG In development http://www.gamedev.n...die-rpg-engine/ |
Personal blog In Portuguese: lotuzgames.wordpress.com |


#29 Arthur Souza   Members   -  Reputation: 1419

Like
1Likes
Like

Posted 18 September 2012 - 12:27 PM

Weird, I just tried

		/// <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; });
			}
			return default(T);
		}

And it compiled perfectly. You just gotta change the signature a bit, since you are receiving a generic T, any type at all, and you actually need a T that is an EngineObject, unless I'm not understanding anything. So it would be:

		public T GetObject<T>(int objectId) where T: EngineObject
		{
			if (m_objectInstances.ContainsKey(typeof(T)))
			{
				List<EngineObject> objectList = m_objectInstances[typeof(T)];
				return  (T) objectList.Find(delegate(EngineObject obj) { return obj.ID == objectId; });
			}
			return default(T);
		}

Or

	    public T GetObject<T>(int objectId) where T: EngineObject
	    {
		    if (m_objectInstances.ContainsKey(typeof(T)))
		    {
			    List<EngineObject> objectList = m_objectInstances[typeof(T)];
			    return  (T) objectList.Find(obj => obj.ID == objectId);
		    }
		    return default(T);
	    }

Edited by Arthur Souza, 18 September 2012 - 12:35 PM.

A.

Lotus - Action RPG In development http://www.gamedev.n...die-rpg-engine/ |
Personal blog In Portuguese: lotuzgames.wordpress.com |


#30 Orangeatang   Members   -  Reputation: 1782

Like
0Likes
Like

Posted 18 September 2012 - 02:18 PM

		public T GetObject<T>(int objectId) where T: EngineObject
		{
			if (m_objectInstances.ContainsKey(typeof(T)))
			{
				List<EngineObject> objectList = m_objectInstances[typeof(T)];
				return  (T) objectList.Find(delegate(EngineObject obj) { return obj.ID == objectId; });
			}
			return default(T);
		}

Or

		public T GetObject<T>(int objectId) where T: EngineObject
		{
			if (m_objectInstances.ContainsKey(typeof(T)))
			{
				List<EngineObject> objectList = m_objectInstances[typeof(T)];
				return  (T) objectList.Find(obj => obj.ID == objectId);
			}
			return default(T);
		}


Thanks for testing that out!

I've found that if I supply the EngineObject type in the function prototype, as you suggested :

public T GetObject<T>(int objectId) where T: EngineObject

it compiles without any errors on x86. I still get the same compile error on the Xenon platform, but I'm halfway there now Posted Image

Edited by Capoeirista, 18 September 2012 - 02:19 PM.


#31 Arthur Souza   Members   -  Reputation: 1419

Like
1Likes
Like

Posted 18 September 2012 - 02:33 PM

Im having some problems converting the project here, and I'm at my work pc, so can't really check the other case :P Weird that this works differently on a different platform.

A.

Lotus - Action RPG In development http://www.gamedev.n...die-rpg-engine/ |
Personal blog In Portuguese: lotuzgames.wordpress.com |


#32 Orangeatang   Members   -  Reputation: 1782

Like
0Likes
Like

Posted 18 September 2012 - 02:37 PM

Im having some problems converting the project here, and I'm at my work pc, so can't really check the other case Posted Image Weird that this works differently on a different platform.

Yeah quite strange... makes more sense that it would be a platform specific problem though, couldn't fathom why it wasn't compiling on x86 Posted Image

Here's the Xenon project file http://www.2shared.com/file/GWlPZyxm/Engine.html

Edited by Capoeirista, 18 September 2012 - 03:35 PM.


#33 Mizu   Members   -  Reputation: 1080

Like
0Likes
Like

Posted 18 September 2012 - 03:25 PM

Now, someone with more knowledge of building xna projects for xbox could probably correct me here, but I noticed the Xenon project uses .NET v2.0. So while the xna assemblies are version 4, the rest of the .net assemblies are version 2.0, and in that version, the List<> doesn't have a the Find() function. That's Probably because the xbox 360 doesn't support anything higher than that version. So if you want the game to work on xbox, you will have to write the game using stuff that exists in .net 2.0 and below only. Someone please correct me if I'm wrong Posted Image

As a side note: it is interesting that linq works there, since it was introduced in .net 3.5 iirc, but looking at the assembly version says its also 2.0... and reading up a bit it seems like xbox supports it.

Edited by Mizu, 18 September 2012 - 03:30 PM.


#34 Orangeatang   Members   -  Reputation: 1782

Like
0Likes
Like

Posted 18 September 2012 - 03:36 PM

Now, someone with more knowledge of building xna projects for xbox could probably correct me here, but I noticed the Xenon project uses .NET v2.0. So while the xna assemblies are version 4, the rest of the .net assemblies are version 2.0, and in that version, the List<> doesn't have a the Find() function. That's Probably because the xbox 360 doesn't support anything higher than that version. So if you want the game to work on xbox, you will have to write the game using stuff that exists in .net 2.0 and below only. Someone please correct me if I'm wrong Posted Image

As a side note: it is interesting that linq works there, since it was introduced in .net 3.5 iirc, but looking at the assembly version says its also 2.0... and reading up a bit it seems like xbox supports it.


Oh nice catch! Thanks for your help :)

#35 Anthracis   Members   -  Reputation: 296

Like
1Likes
Like

Posted 19 September 2012 - 05:18 AM

Now, someone with more knowledge of building xna projects for xbox could probably correct me here, but I noticed the Xenon project uses .NET v2.0. So while the xna assemblies are version 4, the rest of the .net assemblies are version 2.0, and in that version, the List<> doesn't have a the Find() function. That's Probably because the xbox 360 doesn't support anything higher than that version. So if you want the game to work on xbox, you will have to write the game using stuff that exists in .net 2.0 and below only. Someone please correct me if I'm wrong Posted Image

As a side note: it is interesting that linq works there, since it was introduced in .net 3.5 iirc, but looking at the assembly version says its also 2.0... and reading up a bit it seems like xbox supports it.


.net-Framework versions 2.0 to 3.5 use the runtime version 2.0.
Features like Linq are only framework additions that are compiled to byte code running on a 2.0 runtime.

With Frameword version 4.0 a new runtime was introduced.
So assuming that the XBox uses a 2.0 runtime all features available up to Framework 3.5 should be available.

Edit: reformulated the post

Edited by Anthracis, 19 September 2012 - 09:01 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.



PARTNERS