I have two abstract classes:
abstract class Item
{
public string name { get; set; };
public abstract Genre Genre { get; set; };
}
abstract class Genre
{
public string name { get; set; };
}
And their children:
class weapon : Item
{
public virtual GenreWeapon Genre { get; set; };
}
class armor : Item
{
public virtual GenreArmor Genre { get; set; };
}
class GenreWeapon : Genre
{
public int Damage { get; set; };
}
class GenreArmor : Genre
{
public int DamageReduction { get; set; };
}
But I don't know how to do it, I try with
public new virtual GenreWeapon Genre { get; set; };
but if I have Item.Genre
, I get NULL
I try override but VS say it's impossible because its not the same class.
I try a lot of thing but don't know how to do it
Please help me I became crazy.
Thanks
A small example:
a shop contain a lot of items, and the price of a item depend on Genre and Quality
So in the Shop I have something like
List<Item> listItem = new List<Item>();
foreach (Item i in listItem)
{
int prix = i.Genre.Prix;
String Quality = i.Quality;
if (i is Weapon arme)
{
int dégats = arme.Genre.Dégats;
}
if (i is armor armor)
{
int resist = armor.Genre.Armor;
}
}
It's just a quick example, maybe not good ^^
I want everywhere without cast get the damage of a weapon, and the price of all items, and to create a weapon I just have to do:
GenreWeapon sword = (get the GenreWeapon i need);
Weapon w = new Weapon{Genre = sword, name = "old sword of your granny" , quality = "bad"};
But I don't now how deal with Genre in Item and int Weapon without casting everywhere in the code