0

I have some constant error codes in a class:

 public class EDSDK
 {
    public const uint EDS_ERR_UNIMPLEMENTED = 0x00000001;  
    public const uint EDS_ERR_INTERNAL_ERROR = 0x00000002;
    public const uint EDS_ERR_MEM_ALLOC_FAILED = 0x00000003;

}

How can I get the const name from an (hex encoded) value?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
daniel
  • 34,281
  • 39
  • 104
  • 158
  • http://stackoverflow.com/questions/1064213/determine-the-name-of-a-constant-based-on-the-value might be helpful – theduck Jul 15 '15 at 09:25
  • 2
    If you want a mapping between integers and names that's accessible at runtime, perhaps you're looking for an `enum` rather than separate `const`s – Damien_The_Unbeliever Jul 15 '15 at 09:25
  • 1
    (can't add a comment, so im posting it here): see http://stackoverflow.com/questions/1064213/determine-the-name-of-a-constant-based-on-the-value – Patric Jul 15 '15 at 09:26
  • The name is a variable name, it has no place in compiled code. What are you trying to do? – Sayse Jul 15 '15 at 09:26
  • @Sayse: Well it *is* in the compiled code... – Jon Skeet Jul 15 '15 at 09:28
  • What would you expect to happen if you had multiple constants with the same value? What are you trying to achieve? – Jon Skeet Jul 15 '15 at 09:28
  • @Sayse: Not really, to be honest. – Jon Skeet Jul 15 '15 at 09:29
  • @JonSkeet - I mean it isn't really useful to rely on the name of a variable to then be used in any form of program's flow, as Damien mentioned an enum sounds close to what the OP might be after, but without any explanation it would only be guess work – Sayse Jul 15 '15 at 09:31
  • 1
    @Sayse: I agree that an enum would be better, but I think it's also reasonable to rely on publicly-exposed names via reflection. (If it were private, or a local variable, that would be a different matter.) – Jon Skeet Jul 15 '15 at 09:33

1 Answers1

2

If you use enum, it can be done with Enum.ToString():

public enum EDSDK
{
    EDS_ERR_UNIMPLEMENTED = 0x00000001,
    EDS_ERR_INTERNAL_ERROR = 0x00000002,
    EDS_ERR_MEM_ALLOC_FAILED = 0x00000003
}



EDSDK status = (EDSDK)0x00000001;
string statusString = status.ToString();
Ken Hung
  • 190
  • 6