// Project: XnaBreakout, File: EnumHelper.cs // Namespace: XnaBreakout.Helpers, Class: EnumHelper // Path: C:\code\XnaBreakout\Helpers, Author: Abi // Code lines: 11, Size of file: 161 Bytes // Creation date: 24.11.2005 12:46 // Last modified: 02.12.2005 19:33 // Generated with Commenter by abi.exDream.com #region Using directives using System; using System.Collections.Generic; using System.Text; using System.Collections; #endregion namespace XnaBreakout.Helpers { /// /// Enum helper /// class EnumHelper { #region Enum enumerator class /// /// Enum enumerator helper for GetEnumerator, /// this allow us to enumerate enums just like collections. /// public class EnumEnumerator : IEnumerator, IEnumerable { /// /// The enum we use /// public System.Type enumType; /// /// Own index /// public int index; /// /// Length of enum /// public int enumLength; /// /// Create enum enumerator /// public EnumEnumerator(System.Type setEnumType) { enumType = setEnumType; index = -1; enumLength = GetSize(enumType); } // EnumEnumerator(setEnumType) /// /// Own /// public object Current { get { if (index >= 0 && index < enumLength) return Enum.GetValues(enumType).GetValue(new int[] { index }); else // Just return first entry if index is invalid return Enum.GetValues(enumType).GetValue(new int[] { 0 }); } // get } // Current /// /// Move next /// public bool MoveNext() { index++; // Finished? return index < enumLength; } // MoveNext() /// /// Reset /// public void Reset() { index = -1; } // Reset() /// /// Get enumerator /// public IEnumerator GetEnumerator() { return this; } // GetEnumerator() } // class EnumEnumerator #endregion #region Get size /// /// Get number of elements of a enum (accessing enum by type) /// public static int GetSize(System.Type enumType) { return Enum.GetNames(enumType).Length; } // GetSize(enumType) #endregion #region Get enumerator /// /// Get enumerator /// public static EnumEnumerator GetEnumerator(System.Type enumType) { return new EnumEnumerator(enumType); } // GetEnumerator(enumType) #endregion #region Search enumerator /// /// Search enumerator /// /// Type /// Name /// Object public static object SearchEnumerator(Type type, string name) { foreach (object objEnum in GetEnumerator(type)) if (StringHelper.Compare(objEnum.ToString(), name)) return objEnum; // Else not found, just return first! return 0; } // SearchEnumerator(type, name) #endregion #region Get all enum names /// /// Get all names from an enum. /// E.g. If we have an enum with 3 values (A, B and C), /// then return "A, B, C". /// /// Enum type, will be passed to /// GetEnumerator /// String with all enum names public static string GetAllEnumNames(Type type) { // Simplified version return StringHelper.WriteArrayData(GetEnumerator(type)); /*same code, but more complicated: List returnList = new List(); foreach (Enum enumValue in GetEnumerator(type)) returnList.Add(enumValue.ToString()); return StringHelper.WriteArrayData(returnList); */ } // GetAllEnumNames(type) #endregion #region Unit Testing #if DEBUG /*include NUnit.Framework for this /// /// EnumHelper tests to find out if all methods work properly. /// [TestFixture] public class EnumHelperTests { /// /// Test GetAllEnumNames method, which does return all names /// from the enum. We test the RocketCommanderForm MenuButtons. /// [Test] public void TestGetAllEnumNames() { Assert.AreEqual( "Missions, Highscore, Credits, Help, Options, Exit, Back", EnumHelper.GetAllEnumNames(typeof(MenuButton))); } // TestGetAllEnumNames() } // class EnumHelperTests */ #endif #endregion } // class EnumHelper } // namespace XnaBreakout.Helpers