0

Is there a cleaner way of parsing a string that contains a word to describe it's RegistryValueKind? I'm still learning C# and I don't think I'm fully grasping enums.

    private RegistryValueKind ParseValueType(string valueType)
    {
        switch (valueType)
        {
            case "String":
                return RegistryValueKind.String;
            case "Binary":
                return RegistryValueKind.Binary;
            case "DWord":
                return RegistryValueKind.DWord;
            case "ExpandString":
                return RegistryValueKind.ExpandString;
            case "MultiString":
                return RegistryValueKind.MultiString;
            case "QWord":
                return RegistryValueKind.QWord;
            case "Unknown":
                return RegistryValueKind.Unknown;
            default:
                return RegistryValueKind.None;
        }
    }

Surely this isn't standard coding procedure?

mattshu
  • 96
  • 11
  • 1
    Enums are number and can be cast to (int). So I normally use : switch((int)valueType). Then put the enumeration in the case statement. – jdweng May 26 '17 at 05:24
  • @jdweng By default, yes. But they can also 'inherit' from `byte`,`sbyte`,`short`,`ushort`,`int`,`unit`,`long` and `ulong`. So it's not the case that all enums can safely cast to `int`. – Rob May 26 '17 at 05:31

0 Answers0