C # List <baseClass> - any way to store inherited classes inside too?

I am trying to create a simple "inventory" of elements, as in any RPG. I have very simple classes that have properties.

Anyway, I have a base class itemand inheriting from it weapon. itemhas properties (name, value, weight, "important element"), which are also used in weapon, but weaponhas additional properties (attack, defense, speed, manualness).

I have the following code (sorry if the readability is terrible):

static void Main(string[] args)
{   
     List<item> inventory = new List<item>();
     inventory.Add(new weapon("Souleater", 4000, 25.50f, false, 75, 30, 1.25f, 2));
                           //item--------------------------->  weapon--------->

     Console.Write("Name: {0}\nValue: {1}\nWeight: {2}\nDiscardable: {3}\nAttack: {4}\nDefense: {5}\nSpeed: {6}\nHandedness: {7}",
            inventory[0].Name, inventory[0].BValue, inventory[0].Weight, inventory[0].Discard, 
            inventory[0].Atk, inventory[0].Def, inventory[0].Speed, inventory[0].Hands);

     Console.ReadLine();
}

, , weapon, - List<item>. , , - . , , weapon, :

( "shopSystem.item" "Atk" "Atk", "shopSystem.item" )

, , ? "", item, weapon, armour, accessory .., item? , , :

weapon Foo = new weapon("Sword", 200, 20.00f, false, 30, 20, 1.10f, 1);

.

item weapon, - :

class item
{
    #region Region: Item Attributes
    protected string name = "";
    protected int baseValue = 0;
    protected float weight = 0.00f;
    protected bool noDiscard = false;
    #endregion

    public item(string n, int v, float w, bool nd){
        name = n; baseValue = v; weight = w; noDiscard = nd;}

    public string Name{
        get{return name;}
        set{if(value != ""){
            name = value;}
        }//end set
    }

    public int BValue{
        get{return baseValue;}
    }

    public float Weight{
        get{return weight;}
    }

    public bool Discard{
        get{return noDiscard;}
    }
}

class weapon : item
{
    #region Region: Weapon Attributes
    private int atk = 0;
    private int def = 0;
    private float speed = 0.00f;
    private byte hands = 0;
    #endregion

    public weapon(string n, int v, float w, bool nd, int a, int d, float s, byte h) : base(n, v, w, nd){
        atk = a; def =d; speed = s; hands = h;}

    public int Atk{
        get{return atk;}
    }

    public int Def{
        get{return def;}
    }

    public float Speed{
        get{return speed;}
    }

    public byte Hands{
        get{return hands;}
    }

}
+5
5

, , . inventory[0] (weapon)inventory[0].

, , , ( , , ), .

, :

public virtual void LogProperties()
{
Console.Write("Name: {0}\nValue: {1}\nWeight: {2}\nDiscardable: {3}\nAttack",
            this.Name, this.BValue, this.Weight, this.Discard);
}

:

public override void LogProperties()
{
Console.Write("Name: {0}\nValue: {1}\nWeight: {2}\nDiscardable: {3}\nAttack: {4}\nDefense: {5}\nSpeed: {6}\nHandedness: {7}",
            this.Name, this.BValue, this.Weight, this.Discard, 
            this.Atk, this.Def, this.Speed, this.Hands);
}

, :

inventory[0].LogProperties();
+8

, List<item> !

, , , weapon - item.

, i weapon, as, null, (, ). dynamic. , null.

weapon w = inventory[i] as weapon;  // dynamic cast using 'as' operator
if (w != null)
{
     // work with the weapon
}

- is , static.

if (inventory[i] is weapon)
{
     // static cast (won't fail 'cause we know type matches)
     weapon w = (weapon)inventory[i];           

     // work with the weapon
} 
+2

Linq . - . (I.e. inventory[0] weapon, (weapon)inventory[0] .)

, :

var myWeapon = inventory[0] as weapon;

#null . Linq, :

var weapons = inventory.OfType<weapon>();

Where, FirstOrDefault .., .

+1

(, IItem), , , , , T List. , , , .

0

weapon, , .. ((weapon)inventory[0]).Atk). : , , (weapon)inventory[0].Atk; (weapon)(inventory[0].Atk).

(, , , armor). , item, , .

- item , - , - . , , , . , , , ToString() - , :

Console.Write(inventory[0]);

, , ToString() ( , , ToString() , , Console.Write ).

In your weapon class, you must implement the ToString method as follows:

public string ToString() {
  //Put all of the data in here, like in your example
  return String.Format("Name: {0}\n...", name, ...);
}
0
source

All Articles