/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Enum.cs - Demonstrates using a public enum in one class from
// another class
//
// Compile this program with the following command line:
// C:>csc enum.cs
//
namespace nsEnum
{
using System;
public class Enum
{
// Define the enum type
public enum Weekdays
{
Sun, Mon, Tues, Wed, Thurs, Fri, Sat, Count
}
static public void Main ()
{
clsSecond second = new clsSecond();
second.ShowEnum ();
}
}
class clsSecond
{
public void ShowEnum()
{
// Use the class name with the enum name
Console.WriteLine ("Tuesday is day {0} in the week",
(int) Enum.Weekdays.Tues);
}
}
}
Month: February 2011
how a derived class may override an existing enumeration in a base class
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// BaseEnum.cs - Demonstrates how a derived class may override an existing
// enumeration in a base class
//
// Compile this program with the following command line:
// C:>csc baseenum.cs
//
namespace nsEnum
{
using System;
public class clsBaseClass
{
// Define the enum type
public enum Weekdays
{
Sun, Mon, Tues, Wed, Thurs, Fri, Sat
}
static public void Main ()
{
Console.WriteLine ("In base class, Sunday = " +
(int) Weekdays.Sun);
clsNewClass second = new clsNewClass();
second.ShowEnum ();
}
}
class clsNewClass : clsBaseClass
{
public new enum Weekdays
{
Sun = 1, Mon, Tues, Wed, Thurs, Fri, Sat
}
public void ShowEnum()
{
// Use the class name with the enum name
Console.WriteLine ("In derived class, Sunday = " +
(int) Weekdays.Sun);
}
}
}
Illustrates the use of an enumeration that defines the orbital periods of the first four planets in days, using a base type of long
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example2_12.cs illustrates the use of an enumeration
that defines the orbital periods of the first four
planets in days, using a base type of long
*/
public class Example2_121
{
enum PlanetPeriods :long
{
Mercury = 88,
Venus = 225,
Earth = 365,
Mars = 687
}
public static void Main()
{
System.Console.WriteLine("Orbital period for Mars = " +
(long) PlanetPeriods.Mars + " days");
}
}
Illustrates the use of an enumeration that defines the orbital periods of the first four planets in days
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example2_11.cs illustrates the use of an enumeration
that defines the orbital periods of the first four
planets in days
*/
public class Example2_111
{
enum PlanetPeriods
{
Mercury = 88,
Venus = 225,
Earth = 365,
Mars = 687
}
public static void Main()
{
System.Console.WriteLine("Orbital period for Mars = " +
(int) PlanetPeriods.Mars + " days");
}
}
The use of an enumeration that defines the positions of the planets in the solar system relative to the Sun
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example2_10.cs illustrates the use of an enumeration
that defines the positions of the planets in
the solar system relative to the Sun
*/
public class Example2_10
{
enum Planets
{
Mercury = 1,
Venus,
Earth,
Mars,
Jupiter,
Saturn,
Uranus,
Neptune,
Pluto
}
public static void Main()
{
System.Console.WriteLine("Position of Earth = " +
(int) Planets.Earth);
System.Console.WriteLine("Planets.Earth = " +
Planets.Earth);
}
}
Enum Values
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
public class EnumValues
{
// declare the enumeration
enum Temperatures
{
WickedCold = 0,
FreezingPoint = 32,
LightJacketWeather = 60,
SwimmingWeather = 72,
BoilingPoint = 212,
}
static void Main( )
{
System.Console.WriteLine("Freezing point of water: {0}",
(int) Temperatures.FreezingPoint );
System.Console.WriteLine("Boiling point of water: {0}",
(int) Temperatures.BoilingPoint );
}
}
Enum by IEnumerable
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace Enumeration
{
using System;
using System.Collections;
// implements IEnumerable
class ListBoxTest : IEnumerable
{
private string[] strings;
private int ctr = 0;
// private nested implementation of ListBoxEnumerator
private class ListBoxEnumerator : IEnumerator
{
// member fields of the nested ListBoxEnumerator class
private ListBoxTest currentListBox;
private int index;
// public within the private implementation
// thus, private within ListBoxTest
public ListBoxEnumerator(ListBoxTest currentListBox)
{
// a particular ListBoxTest instance is
// passed in, hold a reference to it
// in the member variable currentListBox.
this.currentListBox = currentListBox;
index = -1;
}
// Increment the index and make sure the
// value is valid
public bool MoveNext()
{
index++;
if (index >= currentListBox.strings.Length)
return false;
else
return true;
}
public void Reset()
{
index = -1;
}
// Current property defined as the
// last string added to the listbox
public object Current
{
get
{
return(currentListBox[index]);
}
}
} // end nested class
// Enumerable classes can return an enumerator
public IEnumerator GetEnumerator()
{
return (IEnumerator) new ListBoxEnumerator(this);
}
// initialize the listbox with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[8];
// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
// add a single string to the end of the listbox
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}
// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}
// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
}
public class EnumerationTester
{
public void Run()
{
// create a new listbox and initialize
ListBoxTest currentListBox =
new ListBoxTest("Hello", "World");
// add a few strings
currentListBox.Add("Who");
currentListBox.Add("Is");
currentListBox.Add("John");
currentListBox.Add("Galt");
// test the access
string subst = "Universe";
currentListBox[1] = subst;
// access all the strings
foreach (string s in currentListBox)
{
Console.WriteLine("Value: {0}", s);
}
}
[STAThread]
static void Main()
{
EnumerationTester t = new EnumerationTester();
t.Run();
}
}
}

