I am stuck in a scenario where I can have many game screens, and I would like to be able to select a game screen using a radio button or with a combo box. But the problem is the best way to implement it?
Should I pass in the string of the checkbox or combobox selection to the Factory, or should I use an Enum? If the Enum is the way to go how do I use it? A simple example would be nice thanks.
I like using Enums instead of magical strings in this scenario, because it prevents problems caused by typos, and makes the options available to intellisense.
namespace TheGame
{
// declare enum with all available themes
public enum EnumGameTheme { theme1, theme2 };
// factory class
public class ThemeFactory
{
// factory method. should create a theme object with the type of the enum value themeToCreate
public static GameTheme GetTheme(EnumGameTheme themeToCreate)
{
throw new NotImplementedException();
// TODO return theme
}
}
// TODO game theme class
public class GameTheme { }
}
Code invoking the factory given a theme selected in (say) lstThemes:
// get the enum type from a string (selected item in the combo box)
TheGame.EnumGameTheme selectedTheme = Enum.Parse(typeof(TheGame.EnumGameTheme), (string)lstThemes.SelectedValue);
// invoke the factory method
TheGame.GameTheme newTheme = TheGame.ThemeFactory.GetTheme(selectedTheme);
Code to get available themes as strings:
// get a string array of all the game themes in the Enum (use this to populate the drop-down list)
string[] themeNames = Enum.GetNames(typeof(TheGame.EnumGameTheme));