我有以下代码 -
public static int GetViewLevel(string viewLevelDesc)
{
try
{
switch (viewLevelDesc)
{
case "All":
return 0;
case "Office":
return 10;
case "Manager":
return 50;
default:
throw new Exception("Invalid View Level Description");
}
}
catch (Exception eX)
{
throw new Exception("Action: GetViewLevel()" + Environment.NewLine + eX.Message);
}
}
public static string GetViewLevelDescription(int viewLevel)
{
try
{
switch (viewLevel)
{
case 0:
return "All";
case 10:
return "Office";
case 50:
return "Manager";
default:
throw new Exception("Invalid View Level Description");
}
}
catch (Exception eX)
{
throw new Exception("Action: GetViewLevelDescription()" + Environment.NewLine + eX.Message);
}
}
这两个静态方法使我能够从字符串 ViewLevelDesc 获取 int ViewLevel,反之亦然。我确信我这样做的方式比它需要的要麻烦得多,我正在寻找一些建议如何实现相同的目标但更简洁。int / string 对的列表将显着增加。上面代码中的只是我打算使用的前三个。