Possible false positive for ValidateArgumentsOfPublicMethods

I added an == operator to a class. Its method signature is

publicstaticbooloperator ==(Table table1, Table table2).

FxCop throws the ValidateArgumentsOfPublicMethods error and asks that I check the parameters for null. OK, so I did.

if( table1 == null ||
table2 == null )
{
return false;
}

FxCop is now happy. When I run my unit tests though, I get failures where the tests are throwing a StackOverflowException. Upon further analysis, it appears that testing for null within the == operator introduces an infinite recursion situation.

What is the best way to handle this?

[874 byte] By [Joe2005] at [2008-2-15]
# 1

I had the same problem and created a static method that checks for the null value. Because it takes an object, the == operator of the class is not used:



private static bool IsNull(object table1)
{
return (null == table1);
}

public static bool operator ==(Table table1, Table table2)
{
if (IsNull(table1)) return false;
if (IsNull(table2)) return false;
...
}

DanielRieck at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 2
That is a great solution!

Although it appears to solve my problem, should this situation be considered a false positive for ValidateArgumentsOfPublicMethods as it is currently implemented?

If it flags this situation as a violation of the rule, should it not give advice similar to what you suggested above (only within the == operator method though)?

Joe2005 at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 3

Since your == operator overloading on a reference type should have actually raised another violation; DoNotOverloadOperatorEqualsOnReferenceTypes, I don't think that FxCop should have given any other advice.

DavidM.Kean at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 4

So my situation is actually a false negative for the DoNotOverloadOperatorEqualsOnReferenceTypes rule ? Big Smile

I sure like FxCop. I learn alot about coding each time I use it.

Thanks.

Joe2005 at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 5
This is definitely a good approach, just thought I'd point out that the BCL already provides such a useful static helper:

if (Object.Equals(table1, null)) return false;

etc

Michael

MichaelFanning-MS at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified