【发布时间】:2011-07-20 14:46:08
【问题描述】:
出于通常的原因,我需要在我的应用程序中使用一些常量。
我考虑过的方法:
1) 声明一个枚举:
public enum myOptions
{
MyOption1 = 72,
MyOption2 = 31,
MyOption3 = 44
}
虽然这很适合编程,而且我可以将枚举直接绑定到 DDL,但是当用户看到枚举“名称”时它们很难看 - 用户将看到“MyOption1”,我希望他们看到“我的选项 #1"。
2) 使用列表:
public static List<KeyValuePair<int, string>> myOptions = new List<KeyValuePair<int, string>>
{
new KeyValuePair<int, string>(77, "My Option #1"),
new KeyValuePair<int, string>(31, "My Option #2"),
new KeyValuePair<int, string>(44, "My Option #3")
}
因此,虽然这很好地绑定到 DDL,给了我一个很好的显示值以及一个整数返回值,但我没有任何东西可以测试我的返回值。所以,例如:
if (selectedOption=????) //I'd have to hardcode the Key or Value I want to test for.
3) 我可以构建好的全局/常量程序集:
static myOptions
{
public static KeyValuePair<int, string> MyOption1 = new new KeyValuePair<int, string>(77, "My Option #1");
public static KeyValuePair<int, string> MyOption2 = new new KeyValuePair<int, string>(31, "My Option #2");
public static KeyValuePair<int, string> MyOption3 = new new KeyValuePair<int, string>(44, "My Option #3");
}
这给了我很好的显示名称,很适合编写代码,但据我所知,我无法轻松地将它绑定到 DDL(我必须手动编写代码)。
有没有人有一种优雅的方式来创建易于绑定到 DDL 的常量,我可以在其中有一个漂亮的显示名称?
现在我唯一能想到的就是同时构建枚举和列表,这似乎很烦人。
【问题讨论】: