Sniffle6
Sniffle6

Reputation: 149

Storing a list of strings and arrays

Is this a smart way of doing this?

So I'm making a class within my class called bankItemList and inside of that I have a string and an int.

public List<bankItemList> listOfBankItems = new List<bankItemList>();
[Serializable]
public class bankItemList
{
    public int Stacks;
    public string Name;
    public bankItemList(string name, int amm)
    {
        Name = name;
        Stacks = amm;
    }
}

I then use a small function to add items to the List.

public bool addItemToBankArray(string name, int amm)
{
    foreach (bankItemList i in listOfBankItems)
    {
        if (i.Name == name)
        {
            i.Stacks += amm;
            return true;
        }
    }
    bankItemList item = new bankItemList(name, amm);
    listOfBankItems.Add(item);
    return true;
}

Then I use another function for removing the items.

public void bankItemClicked(GameObject obj)
{
        listOfBankItems.RemoveAt(bankitems.IndexOf(obj));
        bankitems.Remove(obj);
        Destroy(obj);
}

Of course this is trimmed down to only the core functionality. What I'm mainly asking, is creating a serializable class and adding that class to a list a good way of storing a string and an int that is "related" to each other.

Upvotes: 1

Views: 167

Answers (2)

d512
d512

Reputation: 34253

I would probably put all the logic for storing the BankItem objects in your own collection class. That's probably better than having helper methods and another List just floating around. It's better to encapsulate all that together into one class.

public class BankItem
{
    public int Stacks { get; set; }
    public string Name { get; set; }
    public BankItem(string name, int stacks)
    {
        Name = name;
        Stacks = stacks;
    }
}

public class BankItemList
{
    private List<BankItem> bankItems = new List<BankItem>();

    public void AddItem(string name, int stacks)
    {
        BankItem i = bankItems.FirstOrDefault(bi => bi.Name == name);

        if(i != null)
            i.Stacks += stacks;
        else
            bankItems.Add(new BankItem(name, stacks));
    }

    public void RemoveItem(BankItem i)
    {
        bankItems.Remove(i);
    }
}

Upvotes: 1

Programmer
Programmer

Reputation: 125435

Code looks fine. Just few things needs to be changed. Start naming classes/scripts with the first letter capitalized. Your bankItemList should be BankItemList.

IMPORTANT:

You have to change the foreach loop to for loop. Don't use foreach to loop over Lists in the current version Unity. It allocates memory in each loop and the performance is bad.

Your new fixed addItemToBankArray function:

public bool `addItemToBankArray`(string name, int amm)
{
    for (int i =0; i<listOfBankItems.Count; i++ )
    {
        if (listOfBankItems[i].Name == name)
        {
            listOfBankItems[i].Stacks += amm;
            return true;
        }
    }

    bankItemList item = new bankItemList(name, amm);
    listOfBankItems.Add(item);
    return true;
}

Upvotes: 1

Related Questions