a collection of technical fixes and other randon stuff

Spodworld

  • Join Us on Facebook!
  • Follow Us on Twitter!
  • LinkedIn
  • Subcribe to Our RSS Feed

C# How to use an Enum with the switch statemnent


When trying to use enums combined with a switch statement, you may get the follwing error:

"A constant value is expected"

To get around this , try parsing the value so it becomes strongly typed. Using...

Enum.Parse

Then use the switch with the new strongly typed variable.

Example:

var myStat =(RecordStatus) Enum.Parse(typeof(RecordStatus), thisPurchase.Status);
switch (myStat)
            {
                case  RecordStatus.Closed:
                    revokeAllowed = false;
                    revokeDenialReason = "blah!";
                    break;
                case RecordStatus.ProcessedAwaitingIssueOrNotice:
                    //Should be okay
                    break;
                case RecordStatus.Issued:
                    revokeAllowed = false;
                    revokeDenialReason = "blah";
                    break;
                case RecordStatus.ProcessedNoFurtherAction:
                    //Should be okay
                    break;
            }

Add comment

Loading