A List of Lists

Im currently working on a inventory system and i want a list of list for the items. Currently i have

public List<int> itemID = new List<int>();
public List<string> itemName = new List<string>();
public List<int> itemWeight = new List<int>();

But i want itemID to be a list of lists with itemName and itemWeight in it.

1 Answer

1

The easiest way to do this would to have a class or a struct to contain the itemID, itemName and itemWeight: (Im not sure if mono has get/set properties)

public class Item
{
    public int ID { get; set; }

    public int Name { get; set; }

    public int Weight { get; set; }
}

Then you have one list of items:

public List<Item> items = new List<Item>();

@Casper: Yes it does, it's a complete C# library.

How do i do to add a item to the list then? I have a script so when you press E you pick up the item, but how do i get that script that's attached to the item.

Well List<T> has a Add method you can use: items.Add(new Item(1, "My name", 10)) Just make sure you create a constructor for your class (Item) that accepts the 3 parameters