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.

7
  • I would recommend using name instead of number. What does "100" represent? Except of that why not. Commented Feb 29, 2016 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. Commented Feb 29, 2016 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"; Commented Feb 29, 2016 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. Commented Feb 29, 2016 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). Commented Feb 29, 2016 at 15:25

3 Answers 3

5

I suggest using a enum:

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

....

 if (some condition) return Status.One; else return Status.Another; 
Sign up to request clarification or add additional context in comments.

7 Comments

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}
What about public enum Status {Code100=100, Code200=200}?
@Qwerty you can try to use Enum.Parse I think.
@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?
@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: stackoverflow.com/questions/29482/cast-int-to-enum-in-c-sharp
|
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 

2 Comments

I think that public const string Code100 = "100"; will be a better choice; otherwise someone can easily re-assign the value: StatusCodes.Code100 = "bla-bla-bla";
@DmitryBychenko That's right, I'll update the answer.
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

1 Comment

Are you sure you can declare a number in that way? It says identifier expected?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.