Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Simple Enum Parsing
/** * 2017-01-27 CoderHead * This class accepts a <see cref="System.String"/> value and attempts to parse it as an <see cref="System.Enum"/> * in a safe way, accommodating custom values that don't conform to enum member names or int values. */ using System; namespace Rextester { /// <summary> /// Enum values to be parsed. /// </summary> internal enum SampleValues { // Because there's no 0 (zero) member, .NET automatically creates a default and won't error on parse. Beware of zero! One = 1, Four = 4, Five = 5, Thirteen = 13, Fourteen = 14, Eighteen = 18, ThirtyFour = 34, ThirtyNine = 39, SixtyThree = 63, EssBee, SeventyTwo = 72, SeventySix = 76, EightyFive = 85, EightySix = 86, Ninety = 90, NinetyOne = 91, NinetyFour = 94, NinetyFive = 95 } /// <summary> /// Main entry point for the application. /// </summary> public static class Program { #region Methods /// <summary> /// Main entry method for the application. /// </summary> public static void Main(string[] args) { var rawValue = "0"; Console.WriteLine(ParseRawValue(rawValue)); rawValue = "One"; Console.WriteLine(ParseRawValue(rawValue)); rawValue = "005"; Console.WriteLine(ParseRawValue(rawValue)); rawValue = "0x001"; Console.WriteLine(ParseRawValue(rawValue)); rawValue = "thirteen"; Console.WriteLine(ParseRawValue(rawValue)); rawValue = "sb"; Console.WriteLine(ParseRawValue(rawValue)); } /// <summary> /// Parses the raw value into an enum, if possible, and returns a string containing the results. /// </summary> /// <param name="rawValue">A String value containing the code to be parsed.</param> /// <returns>A String value containing user-readable results of the parsing attempt.</returns> private static string ParseRawValue(string rawValue) { var returnValue = string.Empty; int parsedValue; if (int.TryParse(rawValue, out parsedValue)) { returnValue += "The numeric value (" + rawValue + ") is: " + parsedValue.ToString() + ".\r\n"; } else { returnValue += "The raw value (" + rawValue + ") is not numeric.\r\n"; } var enumValue = rawValue.ToSampleValue(); if (enumValue.HasValue) { returnValue += "The enum value is: " + enumValue.Value.ToString() + ".\r\n"; } else { returnValue += "The raw value (" + rawValue + ") is not an enum member.\r\n"; } return returnValue; } #endregion } /// <summary> /// Contains extension methods for strings. /// </summary> internal static class StringExtensions { #region Methods /// <summary> /// Map a raw value to a <see cref="SampleValues"/> enum value. /// </summary> /// <param name="rawValue">A String value containing the raw value.</param> /// <returns>A <see cref="Nullable{SampleValues}"/> member containing the sample value if valid; otherwise, null.</returns> internal static SampleValues? ToSampleValue(this string rawValue) { if (string.IsNullOrWhiteSpace(rawValue)) { return null; } SampleValues parsedEnumValue; // Case-insensitive parsing of string value against enum member name or int value: if (!Enum.TryParse(rawValue.Trim(), true, out parsedEnumValue) || !Enum.IsDefined(typeof(SampleValues), parsedEnumValue)) { // This should be moved into a lookup or custom TypeConverter(?) of some sort: if ("SB".Equals(rawValue.Trim(), StringComparison.InvariantCultureIgnoreCase)) { parsedEnumValue = SampleValues.EssBee; } else { return null; } } return parsedEnumValue; } #endregion } }
run
|
edit
|
history
|
help
0
Test
print star
Prism area
Error
Code which will convert Word into its Binary Format
True and false
Project01
Fórum ➡ Using Regex to insert "item" tags into the unique "list" tag of an XML data ♦
[REDACTED] database V0.1.6
sdfghjikujyhtgrfedwcdf