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), StatusType.Started.ToString());
public static class BamHelper
public enum StatusType{
Started,
CompletedSuccessfully,
CompletedWithErrors
}
public class LogHelper
public enum StatusType{
Started,
CompletedSuccessfully,
CompletedWithErrors
}
Pass the numeric value of enum and get its value
public enum EventType
{
None = 0,
Error = 1,
Warning = 2,
Info = 3
}
string eventTypeValue = Enum.GetName(typeof(EventType), 1);
Result: Error
Example on looping thru the Enum list
public static bool isActionTypePublished(string actionType)
{
foreach (string item in Enum.GetNames(typeof(PublishedActionItemTypeEnum)))
{
if (actionType.Equals(item))
{
return true;
}
}
return false;
}
Example on returning Enum value as string
public static string NextOrchestration(OrchestrationNameEnum orchName)
{
switch (orchName)
{
case OrchestrationNameEnum.Enrichment:
return OrchestrationNameEnum.Analyze.ToString();
case OrchestrationNameEnum.Analyze:
return OrchestrationNameEnum.EnrichForRefer.ToString();
case OrchestrationNameEnum.EnrichForRefer:
return OrchestrationNameEnum.PrepRefer.ToString();
case OrchestrationNameEnum.PrepRefer:
return OrchestrationNameEnum.Refer.ToString();
default:
return string.Empty;
}
}
}
public enum PublishedActionItemTypeEnum
{
UpdateDatabase,
HeaderLookup,
Filing,
}
public enum OrchestrationNameEnum
{
Analyze,
Enrichment,
EnrichForRefer,
PrepRefer,
Refer,
UpdateDatabase,
Filing,
HeaderLookup,
}
Comments
Post a Comment