Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forcing a variable to hold certain values only

Tags:

c#

I am using vs 2012. I have a simple string property

 string _someString;

 public string MyString
  {
     get
       {
          return _someString;
       }

   }

I want this property to hold only certain values. So that when the client uses this property only those certain values can be used.

like image 999
user1970959 Avatar asked Aug 04 '26 13:08

user1970959


2 Answers

It sounds like what you really want is an enum:

public enum MyValues //TODO rename all the things
{
    SomeValue,
    SomeOtherValue,
    FinalValue,
}

Then your property can 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();
like image 161
Servy Avatar answered Aug 07 '26 01:08

Servy


Use an enum as in :

enum MyEnum
{
AllowableValue#1,
AllowableValue#2,
...
}

public MyEnum myEnum { get; set; }

Then populate some UI element with only the values of the enum.

like image 26
Eric H Avatar answered Aug 07 '26 01:08

Eric H