Jump to content
  • Advertisement
Sign in to follow this  
hellz

[.net] Simple Comparisons Question

This topic is 5086 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

Just found something that seems a little odd (at least to me).
if (student["StudentID"].Equals(uint.Parse(txtStudentID.Text)))
   ...

That works properly, but I can't use:
if (student["StudentID"] == uint.Parse(txtStudentID.Text))
   ...

It gives me an Operator '==' cannot be applied to operands of type 'object' and 'uint' error. Can someone explain why that would be the case? Oh, I should probably mention, student["StudentID"] is a uint (System.UInt32). Even double-checked that with GetType(). Thanks in advance, -hellz

Share this post


Link to post
Share on other sites
Advertisement
Though it may be referencing a uint, student["StudentID"] is of type object. The .NET framework does not define an == operator for the object class, but it does define an Equals method. So when you do Equals it resolves the uint.Equals member in the virtual table and compares the two uints.

If you want to use ==, you have to explicitly cast student["StudentID"] to a uint.

Share this post


Link to post
Share on other sites
Ah, thank you very much. See, I found that casting student["StudentID"] to a string meant that the comparison worked, but that looked a little funky and illogical, so thanks for clearing that up for me. [smile]

-hellz

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!