[英]Declare integers inside struct
My code is a service that needs to output different status codes as follows: 我的代码是一项服务,需要输出以下不同的状态代码:
if(something is valid)
{
if(this is found)
return "200";
else
return 300;
}
else
return "100";
There are many such status codes and also occur at various places in the application. 有许多这样的状态代码,它们也出现在应用程序的各个位置。 So, I would like to declare them as constants at one place and use them, so as not to hard code the strings. 因此,我想在一处将它们声明为常量并使用它们,以免对字符串进行硬编码。
something like 就像是
public struct StatusCodes
{
public static string 100 = "100";
public static string 200 = "200";
}
and be able to use it as 并可以将其用作
else return StatusCodes.100
Is there a standard practice or a good way to do so. 是否有标准做法或好的方法呢?
I suggest using a enum : 我建议使用一个枚举 :
public enum Status {
One = 100,
Another = 200
}
.... ....
if (some condition)
return Status.One;
else
return Status.Another;
How about this: 这个怎么样:
public static class StatusCodes
{
public const string Code100 = "100";
public const string Code200 = "200";
}
and you can use it as: 您可以将其用作:
return StatusCodes.Code100
It is better in your case( if you really have a lot of statuses) to create a static class with public fields as: 在您的情况下(如果您确实有很多状态)最好使用公共字段创建一个静态类,如下所示:
public const string myStatus= "100"; public const string myStatus =“ 100”;
So your statuses will be stored in one place. 因此,您的状态将存储在一个地方。 And there you can write MyClass.myStatus 在这里您可以编写MyClass.myStatus
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.