Valus_Paulus
Valus_Paulus

Reputation: 15

C# Get an attribute name from a string

I'm creating a C# console application game which among other things manage a character and his inventory. I have all kinds of items that the player can stock in his inventory. One item in particular cause me trouble, the potion item. It is a item that the player use and that rise one of his stats (i.e Health Points or Magic Points). But the stat to rise is indicated by a string attribute of the Potion class.

Here my code:

the Player class which represent the caracter of the game

public class Player
{
        //Stats of the player
        public int HP;
        public int MP;
        public int Strength;
        public int Defence;

        public void UseItem(Item item) //The parameter is the Item to be used. A potion in my case
        {
            //The potion must rise the stat indicated in its attribute "Bonus Type"
        }
}

the Item class which the Potion class inherited from:

    public class Item
    {
        public string Name; //Name of the item
        public int Bonus; //The stat bonus that the item give to the Player
    }

And Finally the Potion class:

    public class Potion : Item
    {
        public string BonusType; //Says which stat is risen by the potion
    }

Here's my question: What sould I write in the UseItem() method of the Player class to get the attribute "BonusType" of the potion used and so to rise the correct stat according to it?

Upvotes: 0

Views: 887

Answers (3)

user11251194
user11251194

Reputation:

The easy way:


public void UseItem(Item item)
{
switch(item.BonusType)
case "Health Bonus": // one of the "string BonusType"
player.HP = player.HP + Bonus;
break;

case "Magical Bonus": // one of the "string BonusType"
player.HP = player.MP+ Bonus;
break;

default:
// in case of no matchings.
break;
}
}

I want to try your game!

Upvotes: 0

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

you'll need to find out what kind of item you use, in C# 6:

public void UseItem(Item item) 
{
    switch(item)
    {
       case Potion potion:
           if(potion.BonusType == "Whatever")
           {
               //do your stuff
           }
           break;
    }
}

however as @Neijwiert mentioned ... that's not really good design because you then have every responsibility in the player... better would be:

public class Player
{
    //Stats of the player
    public int HP { get; set; }
    public int MP { get; set; }
    public int Strength { get; set; }
    public int Defence { get; set; }

    public void UseItem(Item item) //The parameter is the Item to be used. A potion in my case
    {
        item.Use(this);
    }
}


public abstract class Item
{
    public string Name { get; set;} //Name of the item

    public abstract void Use(Player player);

}

public enum BonusType
{
    HP,
    MP,
    Strength,
    Defence
}

public class Potion : Item
{
    public BonusType BonusType { get; set; }
    public int Amount { get; set; }

    public override void Use(Player player)
    {
        switch (BonusType)
        {
            case BonusType.HP:
                player.HP += Amount;
                break;
            case BonusType.MP:
                player.MP += Amount;
                break;
            case BonusType.Strength:
                player.Strength += Amount;
                break;
            case BonusType.Defence:
                player.Defence += Amount;
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}

Now you can have a whole family of items that manipulate the player and do various effects and the effects are managed by the items and not the player.

Upvotes: 1

Lesiak
Lesiak

Reputation: 25956

Why dont start with a simple cast?

if (item is Potion)
{
    var potion = (Potion) item;
    Console.WriteLine(potion.BonusType);
}

In more recent versionsif c#, you can merge the check with the cast

if (item is Potion potion)
{
    Console.WriteLine(potion.BonusType);
}

Alternatively, switch statement with type check

switch (item)
{
    case Potion potion:
        Console.WriteLine(potion.BonusType);
        break;
}

Upvotes: 0

Related Questions