我正在处理的应用程序有很多枚举。
这些值通常从应用程序的下拉唐斯中选择。
存储这些值的字符串描述的公认方式是什么?
以下是当前的问题:
Public Enum MyEnum
SomeValue = 1
AnotherValue = 2
ObsoleteValue = 3
YetAnotherValue = 4
End Enum
下拉菜单应包含以下选项:
Some Value
Another Value
Yet Another Value (Minor Description)
不是所有的值都符合枚举的名称(例如,其中一个的次要描述),也不是所有的枚举值都是当前值。有些值仅用于向后兼容和显示目的(即打印输出,而不是表单)。
这会导致以下代码情况:
For index As Integer = 0 To StatusDescriptions.GetUpperBound(0)
' Only display relevant statuses.
If Array.IndexOf(CurrentStatuses, index) >= 0 Then
.Items.Add(New ExtendedListViewItem(StatusDescriptions(index), index))
End If
Next
这似乎可以做得更好,我不知道怎么做。
3条答案
按热度按时间qojgxg4l1#
您可以使用
Description
属性(C#程式码,但应该会转译):Source
8i9zcol22#
我会把它们放在一个resource file中,其中的键是枚举名称,可能带有前缀。这样你也可以很容易地本地化字符串值。
cngwdvgl3#
我见过的最常见的方法是用
System.ComponentModel.DescriptionAttribute
注解枚举。然后使用扩展方法获取值(请原谅我的C#,我将在一分钟内转换它):
这是我将其转换为VB的最佳尝试。)