No. You cannot apply rules on parameters like this. That's the entire point behind using an enum
for example.
Try this:
enum Mode
{
bin, hex
}
convertToDec("111011",Mode.hex);
题
I am trying to make a method/function, which can work in two modes: bits or hex.
the user(programmer) should call this method while passing value and mode parameter.
I'd like to limit the mode parameter to a string of "bin" or "hex".
is it possible to generate a compiler error or warning (or pre-compiler error) when the user pass another unwanted value: such as "dec" ?
Example for acceptable parameters:
int decValueFromHex = convertToDec("111011","hex");
int decValueFromBin = convertToDec("111011","bin");
the below is not wanted, and need to generate an error somehow. (before final compilation)
int decValueFromDec = convertToDec("111011","dec");
int decValueFromMood = convertToDec("111011","hey");
解决方案
No. You cannot apply rules on parameters like this. That's the entire point behind using an enum
for example.
Try this:
enum Mode
{
bin, hex
}
convertToDec("111011",Mode.hex);