The only way you can pass a value that is not black or white, is to pass an int
. You can prevent that by adding a check in your setter that calls Enum.IsDefined, eg:
ListType? _listType;
public ListType? List
{
get
{
return _listType;
}
set
{
//Enumb.IsDefined doesn't like nulls
if (value==null || Enum.IsDefined(typeof(ListType),value))
_listType=value;
else
_listType=null;
}
}
You can also combine this with Henk Holterman's answer and add an NA
member equal to 0 in your enumeration. This will probably make your code easier to read.
In both cases your code will have to take care of the special values (NA
or null
). Using a nullable type makes it harder to forget this, but does litter your code a bit.