c# - How to restrict a variable to a fixed set of strings? -


if want restrict values of spicelevel column in database 1, 2 , 3, like

    private enum spicelevel     {         low=1,         medium=2,         hot=3     } 

then in code (int)spicelevel.low pick 1 spice level.

now if have need can accept "red rose","white rose" , "black rose" values of column in database? graceful way handle this?

i thinking of storing them in config file or constants, neither graceful enums. ideas?

update:

the answer here worked me

you can use property this

public string[] allowedroses = new string[] {  "red rose", "white rose" ,"black rose" }; string _rose = "red rose"; public string rose {         {         return _rose;     }     set     {         if (!allowedroses.any(x => x == value))                 throw new argumentexception("not valid rose");         _rose = value;     } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -