Sign in to follow this  

[.net] Simple Comparisons Question

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

This topic is 4867 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.

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