0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pierre
  • 1
  • 1
  • Looks like `Item` should be generic `Item where T : Genre` and then you can do `Weapon : Item` – juharr Apr 13 '21 at 16:17
  • 2
    It's unclear what you are trying to do. Are you trying to access `Genre` on an instance of `Item`? I think you need an [mre] showing how you plan to use these classes. [edit] your answer. – Connor Low Apr 13 '21 at 16:42
  • Don't overload Genre. Assign a new object of a derivative to the property. Also don't reuse names. Both the property type and name the same is asking for problems – JHBonarius Apr 13 '21 at 16:49

1 Answers1

0

I don't fully understand what you want to achieve by implementing abstract classes. First, take a look at what makes the difference between a virtual class and an abstract class. Difference between virtual and abstract methods

Additionally, have a look at the code that shows how you can use the code.

    public class Item
    {
        public string Name { get; set; }
        public Genre Genre { get; set; }
    }
    public class Genre
    {
        public string Name { get; set; }
    }

    public class Weapon : Item
    {
      // No code needed because everything is inherited from Item
    }


    static void Main(string[] args)
    {
        Genre Axe = new Genre() { Name = "Axe" };
        Weapon weapon = new Weapon() { Genre = Axe };
        Console.WriteLine(weapon.Genre.Name);

    }

If you give more information on why you need an abstract class, I will probably be able to help

Dharman
  • 30,962
  • 25
  • 85
  • 135
Troom
  • 431
  • 6
  • 10