jeudi 20 avril 2017

Matching class by enum

Hi I have an abstract class Item. Classes like Food, Weapon, etc inherit by this class. All informations about this items are stored in the database, work of C# Code is match the exact class and match it by Enum which is also stored in the database column as integer. My problem is this stupid code wherever I have to use methods of Food, Weapon etc classes

if ((ItemType)userItem.ItemType == ItemType.Food)
            {
                Food food = new Food(userItem);
                food.UseItem(sender);
            }
            else if ((ItemType)userItem.ItemType == ItemType.Weapon)
            {
                Weapon weapon = new Weapon(userItem);
                weapon.UseItem(sender);
            }

In the parameter of constructor of Food, Weapon etc. classes is the the object from database to let know object about its fields.

Is some kind of stuff that will help me to match this types without this code? It really annoys me when I'm looking at it.






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire