- Using a different type in a C# enum
- public enum EnumWeapons
- {
- Fists = new WeaponFists(),
- Sword = new WeaponSword(),
- Bow = new WeaponBow(),
- Staff = new WeaponStaff()
- }
- public Weapon weapon = (Weapon)EnumWeapons.Fists;
- public enum WeaponType {
- Fists,
- Sword,
- Bow,
- Staff
- }
- public WeaponFactory {
- public Weapon Create(WeaponType weaponType) {
- switch(weaponType) {
- case WeaponType.Fists:
- return new WeaponFists();
- break;
- case WeaponType.Sword:
- return new WeaponSword();
- break;
- case WeaponType.Bow:
- return new WeaponBow();
- break;
- case WeaponType.Staff:
- return new WeaponStaff();
- break;
- default:
- throw new ArgumentOutOfRangeException("weaponType");
- }
- }
- }
- var weapon = weaponFactory.Create(WeaponType.Fists);
- public static class EnumWeapons
- {
- public static readonly Weapon Fists = new WeaponFists();
- public static readonly Weapon Sword = new WeaponSword();
- public static readonly Weapon Bow = new WeaponBow();
- public static readonly Weapon Staff = new WeaponStaff();
- }
- public static class AllWeapons
- {
- public static readonly Weapon Fists = new WeaponFists();
- // etc
- }
- Weapon weapon = AllWeapons.Fists;
- public static class Weapons
- {
- Weapon Fists = new WeaponFists(),
- Weapon Sword = new WeaponSword(),
- Weapon Bow = new WeaponBow(),
- Weapon Staff = new WeaponStaff()
- }
- public enum EnumWeapons
- {
- Fists,
- Sword,
- Bow,
- Staff
- }
- public Weapon EnumWeaponsToWeaponObject(EnumWeapons weapon)
- {
- switch (weapon)
- {
- case EnumWeapons.Fists:
- return new FistWeapon();
- break;
- case EnumWeapons.Sword:
- return new SwordWeapon();
- break;
- case EnumWeapons.Bow:
- return new BowWeapon();
- break;
- case EnumWeapons.Staff:
- return new StaffWeapon();
- break;
- default:
- throw new ArgumentException("Not a supported weapon type!");
- break;
- }
- }
- public class Weapon
- {
- public static readonly Weapon Fists = new WeaponFists();
- public static readonly Weapon Sword = new WeaponSword();
- ...
- }
- var myWeapon = Weapon.Fists;
- namespace Indexer
- {
- class Program
- {
- static void Main(string[] args)
- {
- Persons MyPeople = new Persons();
- Console.WriteLine("MyPeople[0] " + MyPeople[0].Name);
- Console.WriteLine("MyPeople[0] " + MyPeople[1].Name);
- Console.WriteLine("MyPeople[Jim] " + MyPeople["Jim"].Name);
- Console.ReadLine();
- }
- }
- public class Persons
- {
- private List<Person> persons = new List<Person>();
- public int Count { get { return persons.Count; } }
- public Person this[int index]
- {
- get
- {
- return persons[index];
- }
- set
- {
- persons[index] = value;
- }
- }
- public Person this[string name]
- {
- get
- {
- foreach (Person p in persons)
- {
- if (p.Name == name) return p;
- }
- return null;
- }
- }
- public Persons()
- {
- persons.Add(new Person("Jim"));
- persons.Add(new Person("Harry"));
- }
- public class Person
- {
- private string name;
- public string Name { get { return name; } }
- public Person(string Name) { name = Name; }
- }
- }
- }