Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is a correct way of replacing the switch block and Enum in this situation (C#)?
    primarykey
    data
    text
    <p>If it helps, the following question is in the context of a game I am building.</p> <p>In a few different places I have the following scenario. There exists a parent class, for this example called Skill, and I have a number of sub-classes implementing the methods from the parent class. There also exists another parent class that we will call Vocation. The skills need to be listed in different sub-classes of Vocation. However, those skills need to be available for anything in the game that uses any given vocation. </p> <p>My current setup is to have an Enum called Skill.Id, so that Vocation contains a collection of values from that Enum and when an entity in the game takes on that Vocation the collection is passed into another class, called SkillFactory. Skill.Id needs a new entry every time I create a new Skill sub-class, as well as a case in the switch block for the new sub-classes' constructor.</p> <p>i.e.:</p> <pre><code>//Skill.Id Enum{FireSkill,WaterSkill,etc} //SkillFactory public static Skill Create(Skill.Id id) { switch(id) { case Skill.Id.FireSkill: return new FireSkill(); //etc } } </code></pre> <p>This works perfectly fine, but using the enum and switch block as a go between feels like more overhead than I need to solve this problem. Is there a more elegant way to create instances of these Skill sub-classes, but still allows Vocation to contains a collection identifying the skills it can use?</p> <p>Edit: I am fine throwing out the enum and associated switch block, so long as Vocation can contain a collection that allows arbitrary instantiation of the Skill sub-classes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload