-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
  • 90,369
  • 161
  • 124
  • 175
Vasundhara
  • 11
  • 4

1 Answers1

1

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
  • 6,869
  • 4
  • 51
  • 83