Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding C# Conditional statements problem

Tags:

c#

.net

I was writing some code today and something was not working as I expected.

Why does the following code execute even though the condition should have evaluated to false?

alt text http://img215.imageshack.us/img215/3011/agfewrf.gif

I have tried putting braces around the two conditions, and switching their position, but the EndedUsingApplication even still executes.

EDIT:

It has nothing to do with the || or && operators. Look at this...

alt text

Nobody can learn from my mistake unless I post the culprit code, so here it is.

  public static bool operator ==(ActiveApplication a, ActiveApplication b)
     {
     if ((object)a == null || (object)b == null)
        return false;
     return a.process_name == b.process_name && a.window_title == b.window_title;
     }

  public static bool operator !=(ActiveApplication a, ActiveApplication b)
     {
     return a == b ? false : true;
     }

And here is the working code ...

  public static bool operator ==(ActiveApplication a, ActiveApplication b)
     {
     // Casting to object class prevents this comparison operator being executed
     // again and causing an infinite loop (which I think .NET detects and stops
     // but it would still be a huge hole in the logic.
     if ((object)a == null && (object)b == null)
        return true;
     if ((object)a == null ^ (object)b == null)
        return false;
     return a.process_name == b.process_name && a.window_title == b.window_title;
     }

  public static bool operator !=(ActiveApplication a, ActiveApplication b)
     {
     return a == b ? false : true;
     }

The problem appeared to be when the != operator received two null values.

like image 254
Nippysaurus Avatar asked Aug 20 '09 03:08

Nippysaurus


People also ask

What is overriding in C?

When it redefines a function of the base class in a derived class with the same signature i.e., name, return type, and parameter but with a different definition, it is called function overriding.

Is there overriding in C?

c is compiled without override.

What is function overriding with example?

Function overriding in C++ is a feature that allows us to use a function in the child class that is already present in its parent class. The child class inherits all the data members, and the member functions present in the parent class.

What is overriding and overloading in C++?

Overview. In C++, two or more functions can have the same name if the number or the type of parameters are different. This is function overloading, whereas function overriding is the redefinition of a base class function in its derived class with the same signature.


2 Answers

Have you overloaded !=?

like image 127
Jay Riggs Avatar answered Oct 29 '22 21:10

Jay Riggs


Not sure why. But are you sure the running application is compiled using the code you are stepping through. I have seen this sort of thing when the code is different to what is actually being executed.

like image 20
David McEwing Avatar answered Oct 29 '22 20:10

David McEwing