I have two items in my class: One is a public property, and the other is a static method that takes a parameter.
I really do not understand why Visual Studio 2010 is unable to see the difference between these two items.
Could someone explain this one to me?
Here is the code:
public bool IsShipped {
get {
#region ' Test Code '
if (!String.IsNullOrEmpty(TrailerNo) || (TruckDate != Global.NODATE)) {
return true;
}
#endregion
return false;
}
}
public static bool IsShipped(string boxNumber) {
var array = GetCrate(boxNumber);
if (array != null) {
foreach (var item in array) {
if (item.IsShipped) {
return true;
}
}
}
return false;
}
Here is the error:
Error 1 Ambiguity between 'AcpClasses.AcpPackNShip.IsShipped' and 'AcpClasses.AcpPackNShip.IsShipped(string)' C:\Users\cp-jpool\My Projects\VS\Live\Common\Classes\AcpPackShip.cs 242 20 CoilPC
It's possible to refer to a method as a delegate, not just by calling it. For example, the following could would be valid use of the method:
Func<string, bool> myFunction = item.IsShipped;
Given that the method doesn't need to be actually called with parenthesis, there is no way of determining if item.IsShipped
is supposed to refer to the method group for IsShipped
or to be the property IsShipped
.
Even if it were allowed, it would be a point likely to result in confusion. It would be preferable from a code maintenance perspective to have different names for the property/method, even if the compiler were somehow smart enough to know which one to use when (or worse, if it just picked one arbitrarily).
You can overload methods with different signatures, but you can't have a field or property with the same name as a method.
That's because the raw name of a method evaluates to a method group, used when creating a delegate.
This snippet of code clearly shows the problem:
bool b = item.IsShipped;
Func<string, bool> d = item.IsShipped;
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