[.net] Simple Comparisons Question

Started by
1 comment, last by hellz 19 years, 8 months ago
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
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.
Kevin.
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

This topic is closed to new replies.

Advertisement