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...
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.
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.
c is compiled without override.
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.
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.
Have you overloaded !=
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With