Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning (non)constant value to enum

I'm trying to assign a short to a enum like this:;

public enum ValueRepresentation : short
{
   ApplicationEntity = short.Parse("AE"),
   AgeString = short.Parse("AS")
}

This however, obviously, doesn't work. But is there a quick fix to make this work?

Cheers

like image 829
Timo Willemsen Avatar asked Nov 22 '25 03:11

Timo Willemsen


1 Answers

No. Enum values are always compile-time constants. If you want them to be values created by running code, you'll have to autogenerate your source code.

(How exactly would parsing "AS" work anyway?)

Of course, another alternative is to not use enums. They may not be the most appropriate solution for what you're trying to do.

like image 113
Jon Skeet Avatar answered Nov 23 '25 20:11

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!