【发布时间】:2014-01-24 13:50:40
【问题描述】:
我希望我能解释一下我的意思。
namespace BackgroundJob
{
public static class Konstanten
{
public const string closed = "closed";
public const string error = "error";
public static readonly JobStatus jobStatus = new JobStatus();
}
private class JobStatus
{
public string closed { get { return "closed"; } }
public string error { get { return "error"; } }
}
}
我认为最好对常量进行分组,以防万一它们被使用。 这就是我创建“JobStatus”类的原因。我在 switch case 语句中使用常量。这工作正常:
case Konstanten.error:
但这会导致错误:
case Konstanten.jobStatus.error:
ErrorMessage: "A constant value is expected"
你能告诉我如何解决这个问题吗?
【问题讨论】:
-
Konstanten.jobStatus是对对象的引用。该对象只是碰巧不断地为其成员返回相同的值,但这并没有使Konstanten.jobStatus.error成为可以在 switch 语句中使用的常量值。这些必须是可以在编译时查找的真实常量。 -
听起来你应该为我使用枚举而不是 const 字符串