So I am working on a chemistry based project and ran into this tricky problem. I have a bunch of functions doing chemistry type calculations and want to pass avogadros numbe
For a compile-time constant, you need to use the const keyword. But, to use it, your expression itself needs to be a compile-time constant too, as you noticed: you cannot use functions such as Math.Pow.
class Constants
{
public const double avogadrosNum = 6.022E-22;
}
If you cannot or do not want to rework your constant to a compile-time constant, you cannot use it in compile-time contexts, but you could work around that by overloading, so that a runtime value can be used as sort of a default argument:
class chemCalculations
{
public double genericCalc() {
return genericCalc(Constants.avogadrosNum);
}
public double genericCalc(double avogadrosNum) {
// real code here
}
}
(By the way, the value of the constant is either wrong, or has a highly misleading name. It should most likely be 6.022E+23.)