I have this enum:
public enum ContentKey {
Menu = 0,
Article = 1,
FavoritesList = 2
};
This action method:
public ActionResult Edit(string pk, string rk, int row = 0) {
try {
var content = _contentService.Get(pk, rk);
The following class Content which is based on the TableServiceEntity. Note that TableServiceEntity is common to all my data classes.
public class Content : TableServiceEntity
{
public abstract class TableServiceEntity
{
protected TableServiceEntity();
protected TableServiceEntity(string partitionKey, string rowKey);
public virtual string PartitionKey { get; set; }
Is there a way I can check the value of pk matches one of the enum values? What I am not sure about is how I can check this. I assume I need to have the check in the Content class but I am not sure how to override the virtual string and throw an exception when there’s no match.
Update: If possible I would like to do this in the Content class get set but I am not sure how to add a get set to this class.
You can use
Enum.IsDefinedto see if astringmatches an Enum value:You can also define a setter that does not set the backing field unless the new
valueis defined the enum:This is a non-automatic property where you define the backing field yourself. The new value is accessible via the
valuekeyword.