Reputation: 7140
I have a graphics program with a "BlockType" enum. Depending on the state, this might be one of several things: Wood, Stone, Grass, etc.
Originally, there were various texture operations that had to be done for each possibility, but thanks to some refactoring, the enum is only used as an integer, and is no longer needed for switch statements. I.e.:
BlockType someFoo = someObj.blockType;
Texture usedTexture = textureLookupArray[(int) someFoo];
But as a side effect of this, the Enum strings are entirely redundant! I could put the "Stone" Texture at position 5, even if BlockType 5 is defined as "Carpet", "Gravel", or "JQuery"!
My first thought was to simply rewrite BlockType to define "Material1, Material2, etc." instead of hardcoded (and potentially conflicting!) values, but does that actually serve a useful purpose?
Might there be some benefit I'm overlooking to keep BlockType as an enum, or should I just switch it over to a regular Int to reduce confusion?
Upvotes: 3
Views: 153
Reputation:
Why use an array when a dictionary would be much, much better?
private Dictionary<BlockType, Texture> _textures =
new Dictionary<BlockType, Texture>
{
{ BlockType.Wood, new WoodTexture() },
{ BlockType.Metal, new MetalTexture() },
//etc
}
Much more elegant to use
var tex = _textures[someObj.blockType];
Upvotes: 7