-1

I need to handle error codes for the project. Previously, the enums were implemented like the following

enum ErrorCode{
    None=0,
    InvalidUserName = 1,
    InvalidEmail
}

Later, we thought to move to some user defined error code like the following.

"ERR001", "ERR001" .. etc.

For that, I have two options,

What is the best way to do either by enum (which does not support string) or constant?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Vasundhara
  • 13
  • 4

1 Answers1

2

Assign a numerical value to your error reason enum and prefix it with ERR.

public enum Error
{
    None = 0,
    Duplicate = 1,
    MissingDetails = 2,
    MissingFocus = 3,
    NoSampleCode = 4
}

public static string GetErrorCode(Error error)
{
    return $"ERR{(int)error:D3}";
}

Console.WriteLine(GetErrorCode(Error.NoSampleCode)); // prints "ERR004"

If this isn't what you require, you need to provide more information / sample code in your question so we can understand what is needed.

Ray
  • 7,940
  • 7
  • 58
  • 90