I have an enum that looks like the following:
public enum EnumWeapons
{
Fists = new WeaponFists(),
Sword = new WeaponSword(),
Bow = new WeaponBow(),
Staff = new WeaponStaff()
}
Which are derived from the base class Weapon.
Is it possible to use an enum in this way?
If so, will I be able to do something like the following? :
public Weapon weapon = (Weapon)EnumWeapons.Fists;
My attempts haven’t worked as intended, and any help or guidance is appreciated :). Thank you!
This is not possible, the underlying type of an
enumare various integral types. From the documentation:Also, I don’t even think what you are trying to do makes sense. When you want a weapon, do you really want the same instance every time? I doubt it.
What you really want, I think, is a factory. In it’s most naive form, you can say:
Now you can say: