By forcing a variable to hold certain values only
3 answers
It looks like what you really want enum
:
public enum MyValues //TODO rename all the things
{
SomeValue,
SomeOtherValue,
FinalValue,
}
Then your property could be:
private MyValues value;
public MyValues MyValue
{
get { return value; }
}
If you need to get a string representation of that value, just call ToString
on the enum value:
string stringValue = value.ToString();
+3
source to share
I assume you want to get some check on the setter and then:
public string MyString
{
get
{
return _someString;
}
set
{
if (value == "a" || value == "b" /* ... */)
_someString = value;
else
throw new InvalidArgumentException("Invalid value!");
}
}
Make sure to set it via a property and not an actual member variable.
0
source to share