Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 3.96 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using a different type in a C# enum
  2. public enum EnumWeapons
  3. {
  4.     Fists = new WeaponFists(),
  5.     Sword = new WeaponSword(),
  6.     Bow = new WeaponBow(),
  7.     Staff = new WeaponStaff()
  8. }
  9.        
  10. public Weapon weapon = (Weapon)EnumWeapons.Fists;
  11.        
  12. public enum WeaponType {
  13.     Fists,
  14.     Sword,
  15.     Bow,
  16.     Staff
  17. }
  18.  
  19. public WeaponFactory {
  20.     public Weapon Create(WeaponType weaponType) {
  21.         switch(weaponType) {
  22.             case WeaponType.Fists:
  23.                 return new WeaponFists();
  24.                 break;
  25.             case WeaponType.Sword:
  26.                 return new WeaponSword();
  27.                 break;
  28.             case WeaponType.Bow:
  29.                 return new WeaponBow();
  30.                 break;
  31.             case WeaponType.Staff:
  32.                 return new WeaponStaff();
  33.                 break;
  34.             default:
  35.                 throw new ArgumentOutOfRangeException("weaponType");
  36.         }
  37.     }
  38. }
  39.        
  40. var weapon = weaponFactory.Create(WeaponType.Fists);
  41.        
  42. public static class EnumWeapons
  43. {
  44.     public static readonly Weapon Fists = new WeaponFists();
  45.     public static readonly Weapon Sword = new WeaponSword();
  46.     public static readonly Weapon Bow = new WeaponBow();
  47.     public static readonly Weapon Staff = new WeaponStaff();
  48. }
  49.        
  50. public static class AllWeapons
  51. {
  52.     public static readonly Weapon Fists = new WeaponFists();
  53.     // etc
  54. }
  55.  
  56. Weapon weapon = AllWeapons.Fists;
  57.        
  58. public static class Weapons
  59. {
  60.     Weapon Fists = new WeaponFists(),
  61.     Weapon Sword = new WeaponSword(),
  62.     Weapon Bow = new WeaponBow(),
  63.     Weapon Staff = new WeaponStaff()
  64. }
  65.        
  66. public enum EnumWeapons
  67. {
  68.     Fists,
  69.     Sword,
  70.     Bow,
  71.     Staff
  72. }
  73.  
  74. public Weapon EnumWeaponsToWeaponObject(EnumWeapons weapon)
  75. {
  76.         switch (weapon)
  77.         {
  78.             case EnumWeapons.Fists:
  79.                 return new FistWeapon();
  80.                 break;
  81.             case EnumWeapons.Sword:
  82.                 return new SwordWeapon();
  83.                 break;
  84.             case EnumWeapons.Bow:
  85.                 return new BowWeapon();
  86.                 break;
  87.             case EnumWeapons.Staff:
  88.                 return new StaffWeapon();
  89.                 break;
  90.             default:
  91.                 throw new ArgumentException("Not a supported weapon type!");
  92.                 break;
  93.         }
  94.  
  95. }
  96.        
  97. public class Weapon
  98. {
  99.     public static readonly Weapon Fists = new WeaponFists();
  100.     public static readonly Weapon Sword = new WeaponSword();
  101.    ...
  102. }
  103.        
  104. var myWeapon = Weapon.Fists;
  105.        
  106. namespace Indexer
  107.     {
  108.         class Program
  109.         {
  110.             static void Main(string[] args)
  111.             {
  112.                 Persons MyPeople = new Persons();
  113.                 Console.WriteLine("MyPeople[0] " + MyPeople[0].Name);
  114.                 Console.WriteLine("MyPeople[0] " + MyPeople[1].Name);
  115.                 Console.WriteLine("MyPeople[Jim] " + MyPeople["Jim"].Name);
  116.                 Console.ReadLine();
  117.             }
  118.         }
  119.         public class Persons
  120.         {
  121.             private List<Person> persons = new List<Person>();
  122.             public int Count { get { return persons.Count; } }
  123.  
  124.             public Person this[int index]
  125.             {
  126.                 get
  127.                 {
  128.                     return persons[index];
  129.                 }
  130.                 set
  131.                 {
  132.                     persons[index] = value;
  133.                 }
  134.             }
  135.             public Person this[string name]
  136.             {
  137.                 get
  138.                 {
  139.                     foreach (Person p in persons)
  140.                     {
  141.                         if (p.Name == name) return p;
  142.                     }
  143.                     return null;
  144.                 }
  145.             }
  146.             public Persons()
  147.             {
  148.                 persons.Add(new Person("Jim"));
  149.                 persons.Add(new Person("Harry"));
  150.             }
  151.             public class Person
  152.             {
  153.                 private string name;
  154.                 public string Name { get { return name; } }
  155.                 public Person(string Name) { name = Name; }
  156.             }
  157.         }
  158.     }