I am working on a .net Core project and am trying to get it to work with Travis CI. At this point I am getting a mysterious error that I cannot make sense of based on how I interpret the message and the code usage. Can anyone help give me some guidance as to what the actual problem is?
I am developing in VS 2019 on a Windows 10 machine and the project builds fine on my local machine with no errors or warnings. On Travis it is built on Ubuntu Linux.
The error message in Travis CI (also highlighted in the screen shot that includes other contextual info):
NumberTypeExtensions/NumberTypeExtensionLibrary.cs(348,31): error CS0176: Member 'double.IsNegative(double)' cannot be accessed with an instance reference; qualify it with a type name instead [/home/travis/build/MarkPThomas/MPT.Math-.netCore/MPT.Math/MPT.Math.csproj]
The extension method where the error occurs (by calling power.IsNegative(0)
):
public static double Pow(this double value, double power)
{
if (value == 0 && power.IsNegative(0))
{
throw new DivideByZeroException($"{value}^{power} results in a division by zero, which is undefined.");
}
return NMath.Pow(value, power);
}
The called extension method:
public static bool IsNegative(this double value, double tolerance = Numbers.ZeroTolerance)
{
if (value.IsZero(tolerance)) { return false; }
return (value < NMath.Abs(tolerance));
}
NMath
& Numbers
are static methods being called on other static classes.
If I change the call to power.IsNegative()
then the main project compiles fine, but then the error is just pushed down to the unit tests, where the same error is thrown here when calling number.IsNegative(tolerance)
:
[TestCase(-0.001, 0.1, ExpectedResult = false)]
[TestCase(0, 0.1, ExpectedResult = false)]
[TestCase(0.001, 0.1, ExpectedResult = false)]
[TestCase(-0.001, 0.0001, ExpectedResult = true)]
[TestCase(-0.001, -0.0001, ExpectedResult = true)]
public bool IsNegative_Double_Custom_Tolerance(double number, double tolerance)
{
return number.IsNegative(tolerance);
}
I'm not sure if this is relevant, but at least by line number in the compiled code, there is a mirrored function IsPositive
that is defined earlier and tested earlier that doesn't throw any errors (yet?). It is basically identical in form and usage.
Extension method is just syntax sugar which allows user to call method defined in other classes from object directly. It's called like instance method, but really it's implemented as static method. I am not sure, but I think you are getting this error because Double.IsNegatie(double)
really exists (see here), so method with same signature as your extension method already exists and runtime thinks ,that you are actually trying to call .Net framework static method instead of your extension method (note that you are not passing your optional parameter while calling, so exactly same signature) and throws error, because static method can't be called from instance. I think just renaming that method will work. Sorry if my theory is wrong