How do you make an 'enum' that has data tied to it?

前端 未结 6 1907
深忆病人
深忆病人 2021-01-23 02:28

I have a Vote class and one of the properties it can have is a vote type. Such as unanimous, a 3/4 vote, a simply majority, etc. Each type needs to have a string associated with

6条回答
  •  逝去的感伤
    2021-01-23 02:54

    You can use enums if you want but you need to decide how to make the link between the enum value and what you want to display. For example, an enum value of SimpleMajority you would want displayed as "Simple Majority". One way to do this is using the Description attribute and a helper class as described here.

    However, you might find it easier to set up a lightweight collection class to store vote type values and their description. This could be as simple as a Dictionary You will probably find this a more straightforward approach.

提交回复
热议问题