0

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.

sukesh
  • 2,379
  • 12
  • 56
  • 111
  • I would recommend using name instead of number. What does "100" represent? Except of that why not. – Divisadero Feb 29 '16 at 15:20
  • 2
    If you're just using numbers then maybe an enum is a lot better... else instead an struct use an static class with constants. – Gusman Feb 29 '16 at 15:21
  • 1
    Identifiers can't start with a number so you can't do `StatusCodes.100`, but maybe you could name them by their description and give their value the real error code? i.e. `public static string SomeCertainError = "100";` – squill25 Feb 29 '16 at 15:21
  • subs `something is valid` with `something == valid`. Usually numbers (integers/longs) are more efficient than strings. `enum` is useful. See `Enum.Parse` etc for advanced features. Just reading up on `enum` and examples should help you out a lot here. – ebyrob Feb 29 '16 at 15:24
  • 1
    The point of "not to hard code values" is to improve code readability. You shouldn't use constants just for using constants. Now you want to declare a constant named "100" that contains 100. That won't improve readability of the code, hence, doesn't make sense. Moreover, if you change the value in the future, readability become worse (e.g. constant "100" that contains number 200). – enkryptor Feb 29 '16 at 15:25
  • @Divisadero. 100 stands for not one but multiple situations like error at the third party service. So, using text is not a optimum option here. – sukesh Feb 29 '16 at 15:26
  • "error at the third party service" - but that error codes have meanings, don't they? Name codes by their meanings. For instance, HttpNotFound = 404. – enkryptor Feb 29 '16 at 15:31

3 Answers3

5

I suggest using a enum:

  public enum Status {
    One = 100,
    Another = 200
  }

....

  if (some condition)
    return Status.One;
  else
    return Status.Another;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • The problem is, cant use text as 'One', 'Another'. I should be able to read/call them by numbers alone. eg: `public enum Status {100=100}` – sukesh Feb 29 '16 at 15:24
  • 2
    What about public enum Status {Code100=100, Code200=200}? – SunsetQuest Feb 29 '16 at 15:25
  • @Qwerty you can try to use Enum.Parse I think. – Ian Feb 29 '16 at 15:26
  • 2
    @Qwerty As I stated before, you can't have names such as `100` or `200` because they can't start with numbers. Also it's bad practice to do what you're doing because what if someone else reads your code? How do they know what code 100 or 200 is? – squill25 Feb 29 '16 at 15:26
  • 1
    @Qwerty you can convert from string or integer to get an enum. (Just cast for int: `Status s = (Status)100;` or `Status s = (Status)int.Parse("100");`. See this thread: http://stackoverflow.com/questions/29482/cast-int-to-enum-in-c-sharp – ebyrob Feb 29 '16 at 15:28
  • Sunsetquest's solution seems the most easy/suitable for my case now. Thank you – sukesh Feb 29 '16 at 15:38
  • Anytime. Glad to help! =) I cannot think of a way to do a "Status.100". Maybe someone else will think of something. – SunsetQuest Feb 29 '16 at 15:44
2

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
romanoza
  • 4,775
  • 3
  • 27
  • 44
1

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

Katia
  • 619
  • 6
  • 15