I have an abstract class that declares several constants, and then those are used as defaults for some parameters in methods later
public abstract class Foo
{
public const int ONE = 1;
public const int TWO = 2;
public void CustomUpdate()
{
...
Bar("some string", true); // ERROR HERE
...
}
protected void Bar(string one, bool two, int three = ONE)
{
....
}
}
For some reason I kept getting an error when I try to call Bar. It said something like “the name Bar does not exist in the current context.” I noticed that if I did the following, it would work.
public abstract class Foo
{
public const int ONE = 1;
public const int TWO = 2;
public void CustomUpdate()
{
...
Bar("some string", true);
...
}
public void Bar(string one, bool two, int three = 1) // see I changed the parameter "three"
{
....
}
}
I thought constants were figured out at compile-time, thus could be used as default parameters? I know that the Mono .NET version is out of date compared to the current .NET version, but I wouldn’t think that constants would ever be figured out at run-time.
In order to still be able to use the constants, I just created overloading methods for the “Bar” method name and I don’t have any errors. Here is the current code sample in case anyone sees this problem in the future:
public abstract class Foo
{
public const int ONE = 1;
public const int TWO = 2;
public void CustomUpdate()
{
...
Bar("some string", true);
...
}
public void Bar(string one, bool two, int three)
{
....
}
public void Bar(string one, bool two)
{
Bar(one, two, ONE); // I want the constant "ONE" to be the default parameter
}
}
Either way, is this a bug, or am I missing something?
Thank you in advance!