# Storing Information In Array

**URL:** <https://discussions.unity.com/t/storing-information-in-array/601152>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [October 12, 2015, 8:17am UTC](https://discussions.unity.com/t/storing-information-in-array/601152 "2015-10-12T08:17:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![solideuk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@solideuk](https://discussions.unity.com/u/solideuk)\
**Post date:** [October 12, 2015, 8:17am UTC](https://discussions.unity.com/t/storing-information-in-array/601152/1 "2015-10-12T08:17:19Z")

</div>

Hi

I am trying to set up an array to store information relating to a particular item.

If I wish to store information on a particular item that contains both a string and integer, how would I go about doing this?

For example if I wish to create a table for fruits with quantity sold, would I use a multidimensional array and select string as type and convert the string to integer when required?

Many thanks

---

<div class="post-metadata">

**Author:** ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)\
**Post date:** [October 12, 2015, 8:37am UTC](https://discussions.unity.com/t/storing-information-in-array/601152/2 "2015-10-12T08:37:04Z")

</div>

sounds more like a dictionary, but you’ve not explained the use case in much detail…

---

<div class="post-metadata">

**Author:** ![martinmr](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@martinmr](https://discussions.unity.com/u/martinmr)\
**Post date:** [October 12, 2015, 8:45am UTC](https://discussions.unity.com/t/storing-information-in-array/601152/3 "2015-10-12T08:45:12Z")

</div>

For an array that stores a name and a value

```csharp
public class ItemInformation {
    public string name;
    public int value;
}

public class YourOtherClass : MonoBehaviour {
  
    private ItemInformation[] items = new ItemInformation[5];

    private void DoSomethingMethod() {

        items[X].name = "string";
        items[X].value = 42;

        //or

        for (int i = 0; i < items.Length; i++) {
            items[i].name = "string";
            items[i].value = 42;
        }
    }
}

```

But like @LeftyRighty said, depending on your use case also a Dictonary would work or if you are adding items to something also a List would be ok.

---

<div class="post-metadata">

**Author:** ![solideuk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@solideuk](https://discussions.unity.com/u/solideuk)\
**Post date:** [October 12, 2015, 8:47am UTC](https://discussions.unity.com/t/storing-information-in-array/601152/4 "2015-10-12T08:47:22Z")

</div>

> [@LeftyRighty](#):
>
> sounds more like a dictionary, but you’ve not explained the use case in much detail…

Thanks.

Example of scenario:

Item Type: Quantity:  
Apple 10  
Orange 15

The item types will be predefined, the quantity will be changed accordingly.

---

<div class="post-metadata">

**Author:** ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)\
**Post date:** [October 12, 2015, 8:55am UTC](https://discussions.unity.com/t/storing-information-in-array/601152/5 "2015-10-12T08:55:17Z")

</div>

apple etc. only ever exists once? I’d definitely go with a dictionary

if you’re after something more like

monday apple 10  
tuesday apple 5

etc.

you might want to go with an array/list of structs that hold the 3 bits of information.

---

<div class="post-metadata">

**Author:** ![solideuk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@solideuk](https://discussions.unity.com/u/solideuk)\
**Post date:** [October 12, 2015, 8:57am UTC](https://discussions.unity.com/t/storing-information-in-array/601152/6 "2015-10-12T08:57:21Z")

</div>

> [@martinmr](#):
>
> For an array that stores a name and a value
> 
> ```csharp
> public class ItemInformation {
> public string name;
> public int value;
> }
> 
> public class YourOtherClass : MonoBehaviour {
>  
> private ItemInformation[] items = new ItemInformation[5];
> 
> private void DoSomethingMethod() {
> 
> items[X].name = "string";
> items[X].value = 42;
> 
> //or
> 
> for (int i = 0; i < items.Length; i++) {
> items[i].name = "string";
> items[i].value = 42;
> }
> }
> }
> 
> ```
> 
> But like @LeftyRighty said, depending on your use case also a Dictonary would work or if you are adding items to something also a List would be ok.

Thanks will give this a go.

How efficient is this method (for tablet)?

As the item name will be predefined searching for a particular record should be easy as I can simply access using items[0] etc

---

<div class="post-metadata">

**Author:** ![solideuk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@solideuk](https://discussions.unity.com/u/solideuk)\
**Post date:** [October 12, 2015, 8:59am UTC](https://discussions.unity.com/t/storing-information-in-array/601152/7 "2015-10-12T08:59:24Z")

</div>

> [@LeftyRighty](#):
>
> apple etc. only ever exists once? I’d definitely go with a dictionary
> 
> if you’re after something more like
> 
> monday apple 10  
> tuesday apple 5
> 
> etc.
> 
> you might want to go with an array/list of structs that hold the 3 bits of information.

That’s correct apple etc will only appear once.  
It’s likely that the column numbers would increase, it may be beneficial in that case to go with array.

The example above, that is an array and not an array list, is that correct?

Edit:

For the scenario described, would a playerpref method be better than using an array method?
