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";
So your statuses will be stored in one place. And there you can write MyClass.myStatus
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.