chaoswolf71
chaoswolf71

Reputation: 17

C# - Struggling to access variables for object within a list

I'm have tried looking around for a solution but to no avail.

Why can't i access any of the variables within the created items in the list

(inv.inventory[i].Name as seen near the bottom)

If needed i can post all my code although it is 569 lines.

public class Item
{
    //All Item Attributes
}

public class PotionHP : Item
{
    // Specific Attributes
    public string Name = "HP Potion";
    public string Desc = "Restores HP by 10.";
    public int Cost = 8;
    public int Modifier = 10;
}

public class PotionAT : Item
{
    // Specific Attributes
    public string Name = "AT Potion";
    public string Desc = "Restores Attack by 1.";
    public int Cost = 8;
    public int Modifier = 10;
}

public class Revive : Item
{
    // Specific Attributes
    public string Name = "Revive";
    public string Desc = "Revives upon death with 5 HP.";
    public int Cost = 8;
    public int Modifier = 10;
}
        
public class Inventory
{
    public List<Item> inventory = new List<Item>();

    public Inventory()
    {
        inventory.Add(new PotionHP());
        inventory.Add(new PotionAT());
        inventory.Add(new PotionHP());
        inventory.Add(new Revive());
     }
}

for (int i = 0; i < inv.inventory.Count; i++)
{
    //Why can't i access inv.inventory[i].Name?
    Console.WriteLine($"{inv.inventory[i].Name}");
}

Upvotes: 0

Views: 59

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

Because you don't have Name field in base class. Move it to base class. Also I recommend to use properties instead of public fields:

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

Also seems like you don't need inheritance here. You just providing values to base class fields. You can use Creation Methods for that.

Of course, you can use other options to create items with predefined field values. But anyway - you don't need new classes if your 'classes' differ only by values of fields. You need different instances of one class in that case.

public class Item
{
    public string Name { get; set; }
    public string Desc { get; set; }
    public int Cost { get; set; }
    public int Modifier { get; set; }

    public static Item CreatePotionAT()
    {
        return new Item
        {
           Name = "AT Potion",
           Desc = "Restores HP by 10.",
           Cost = 8,
           Modifier = 10
        };
    }

    // etc
}

Or even:

public class Item
{
    public string Name { get; }
    public string Desc { get; }
    public int Cost { get; }
    public int Modifier { get; }

    public Item(string name, string desc, int cost, int modifier)
    {
        Name = name;
        Desc = desc;
        Cost = cost;
        Modifier = modifier;
    }

    public static Item CreatePotionHP()
    {
        return new Item("HP Potion","Restores HP by 10.", 8, 10);
    }

    // etc
}

And inventory:

public Inventory()
{
    inventory = new List<Item>
    {
       Item.CreatePotionHP(), // creation method
       new Item("AT Potion","Restores Attack by 1.", 8, 10), // constructor
       Item.CreatePotionHP(),
       new Item("Revive","Revives upon death with 5 HP.", 8, 10)
    }
 }   

Upvotes: 4

Mong Zhu
Mong Zhu

Reputation: 23732

You are dangling around the inheritance that you are using. Up to now the inheritance from Item does not have any advantage. Since all your children classes have the same parameters you can simply write the properties into the base class and leave only the initialization in the constructors of the derived classes:

public class Item
{

    public string Name = "HP Potion";
    public string Desc = "Restores HP by 10.";
    public int Cost = 8;
    public int Modifier = 10;

    //All Item Attributes
}
public class PotionHP : Item
{
    public PotionHP()
    {
        // Specific Attributes
        Name = "HP Potion";
        Desc = "Restores HP by 10.";
        Cost = 8;
        Modifier = 10;
    }
}
public class PotionAT : Item
{
    public PotionAT()
    {
        Name = "AT Potion";
        Desc = "Restores Attack by 1.";
        Cost = 8;
        Modifier = 10;
    }
}
public class Revive : Item
{
    public Revive()
    {
        // Specific Attributes
        Name = "Revive";
        Desc = "Revives upon death with 5 HP.";
        Cost = 8;
        Modifier = 10;
    }
}

Upvotes: 1

Related Questions