I’m working on a dll which contains a type which shoul be able to represent an integer value from 32 to 126 and is called “PByte” (for Printable Byte). The problem is that I want to protect the user from initializing the type with e.g. 1000. This should not throw an exception. It should prevent from compiling, like Visiual Studio does trying to initialize for example a byte with 256. The type is initialized in the constructor.
public PByte(int value)
{
/* if (value < 32 || value > 126)
throw new ArgumentException("\"" + value + "\" is an invalid value!"); */
this._value = value;
}
This
PByte pb = new PByte(2000);
should not be compilable.
You want a runtime exception to occur on compile time? That’s impossible!
Should
PByte pb = new PByte(get399());compile? No, but how can you know whatget399()does without running the program?But first of all you should make the parameter be a
bytetype. That will produce compile time exceptions for numbers outside 0-255.