Skip to main content

Posts

Showing posts with the label C#

C#: How To – Personal notes on Enum

enum Color { Red, Green, Blue }; int i = ...; switch (i) { // cast required! Aarg! case (int)Color.Red: break; case (int)Color.Green: break; case (int)Color.Blue: break; } enum Color { Red, Green, Blue }; Color c = ...; switch (c) // Governing type is ‘Color’ not ‘int’ so ... { // ... no cast required case Color.Red: break; case Color.Green: break; case Color.Blue: break; } Retrieve Numeric representation of enum If numeric value is know,you can get the string constant using the following syntax TraceLevel tl = (TraceLevel)0; Trace.WriteLine("Trace Level: 0 " + tl.ToString()); To get the numeric value of Enum, use the following syntax int i1 = (System.Int32)TraceLevel.Verbose; Convert One Enum type to another Enum type. Must make sure both enum types share the same name BamHelper.StatusType sharedStatusType = (BamHelper.StatusType)Enum.Parse(typeof(BamHelper.StatusType), Sta...