Social Networks

Enum types implicitly inherit the System.Enum type in the Base Class Library (BCL). This also means that you can use the members of System.Enum to operate on enum types. This section does just that, showing some useful tips and tricks to use with enums in your programs.

A common requirement with enums is to convert between the enum and a variable of its base type.

enum-value-converter

For example, if you are getting input in the form of an int from a user or a file stream, then you can cast it to an enum and use it in a meaningful way in your program. You can also get a complete list of enum member names or enum values, which is useful if you have logic that needs to iterate through every enum member. Listing 17-3 shows how to perform conversions between enums and their base types and how to use some of the System.Enum type members.

 Code Example Enum Conversions and using the System.Enum

using System;
// declares the enum
public enum Volume : byte
{
Low = 1,
Medium,
High
}

// shows different ways
// to work with enums
class Enumtricks
{
static void Main(string[] args)
{
// instantiate type
Enumtricks enumtricks = new Enumtricks();

// demonstrates explicit cast
// of int to Volume
enumtricks.GetEnumFromUser();

// iterate through Volume enum by name
enumtricks.ListEnumMembersByName();

// iterate through Volume enum by value
enumtricks.ListEnumMembersByValue();

Console.ReadLine();
}

// demonstrates explicit cast
// of int to Volume
public void GetEnumFromUser()
{
Console.WriteLine("n----------------");
Console.WriteLine("Volume Settings:");
Console.WriteLine("----------------n");

Console.Write(@"
1 - Low
2 - Medium
3 - High

Please select one (1, 2, or 3): ");

// get value user provided
string volString = Console.ReadLine();
int volInt = Int32.Parse(volString);

// perform explicit cast from
// int to Volume enum type
Volume myVolume = (Volume)volInt;

Console.WriteLine();

// make decision based
// on enum value
switch (myVolume)
{
case Volume.Low:
Console.WriteLine("The volume has been turned Down.");
break;
case Volume.Medium:
Console.WriteLine("The volume is in the middle.");
break;
case Volume.High:
Console.WriteLine("The volume has been turned up.");
break;
}

Console.WriteLine();
}

// iterate through Volume enum by name
public void ListEnumMembersByName()
{
Console.WriteLine("n---------------------------- ");
Console.WriteLine("Volume Enum Members by Name:");
Console.WriteLine("----------------------------n");

// get a list of member names from Volume enum,
// figure out the numeric value, and display
foreach (string volume in Enum.GetNames(typeof(Volume)))
{
Console.WriteLine("Volume Member: {0}n Value: {1}",
volume, (byte)Enum.Parse(typeof(Volume), volume));
}
}

// iterate through Volume enum by value
public void ListEnumMembersByValue()
{
Console.WriteLine("n----------------------------- ");
Console.WriteLine("Volume Enum Members by Value:");
Console.WriteLine("-----------------------------n");

// get all values (numeric values) from the Volume
// enum type, figure out member name, and display
foreach (byte val in Enum.GetValues(typeof(Volume)))
{
Console.WriteLine("Volume Value: {0}n Member: {1}",
val, Enum.GetName(typeof(Volume), val));
}
}
}

Ref – http://www.csharp-station.com/Tutorials/lesson17.aspx

None found.

Leave a Reply

Your email address will not be published. Required fields are marked *

Allowed Tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>