Reputation: 1337
I'm still relatively new to C# and the answer to this is probably pretty obvious, but I'm struggling to solve it
I have an enum defined as follows:
enum Request {
None = 0,
GetData,
SendData,
some other values...
};
I then have two variables both defined as type Request as follows
Request currentRequest; // This is a class member variable
Request request; // This is a local variable within a method.
The first variable is assigned using currentRequest = Request.GetData; The second local variable request is assigned using request = (Request)data, where data is byte of value 1 (as the value is being decoded from a buffer of USB data being received). I don't know if this is relevant but mention it just in case.
I want to compare them within the method in which the second variable is declared, I originally did it like this:
if(request != currentRequest)
{
// Throw an exception
}
This works most of the time, but occaisionally the exception is thrown because the two values don't equate, however when I check them in the debugger they are both equal to Request.GetData. This got me thinking that the variables may be pointers to objects rather than values so I tried also using...
!request.Equals(currentRequest)
and
request.CompareTo(currentRequest)!=0
and similarly both of these work most of the time but occaisionally fail even though when the values are checked in the debugger they are both Request.GetData.
What's really confused me is that it works most of the time, just fails occaisionally - I'd expect it to either work or not work consistently.
Any ideas?
Upvotes: 1
Views: 128
Reputation: 1500835
No, if the variables are genuinely of type Request
then that's a value type, and using ==
should be absolutely fine.
I suspect that the debugger is showing you something slightly odd. I suggest you make the exception include both request
and currentRequest
to show you what's going on.
Upvotes: 1