0

Is there any way to make this happen - it's definitely a curiosity rather than a necessity and is basically just to shorten written code... but it might make it easier for adding constructor variables on a loop...

Instead of:

public class item_holder
{
    public item ITEM1 = new item();
    public item ITEM2 = new item();
    public List<item> myItems = new List<item>();

    public item_holder()
    {
        myItems.Add(ITEM1);
        myItems.Add(ITEM2);
    }
}

Can we have something like this?

public class item_holder
{
    public item ITEM1;
    public item ITEM2;
    public List<item> myItems = new List<item>();

    public item_holder()
    {
        myItems.Add(ITEM1);
        myItems.Add(ITEM2);

        foreach(item i in myItems)
        {
            i = new item();
        }
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
jamheadart
  • 5,047
  • 4
  • 32
  • 63
  • Why are you storing the same `item`s twice in the class in the first place? – UnholySheep Jul 14 '18 at 17:15
  • https://stackoverflow.com/a/18122610/442444 – Carbine Jul 14 '18 at 17:20
  • Why does `item_holder` which would appear to be a collection class expose the inner collection? There is also no need for temp vars for collection objects. [Guidelines for Collections](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/guidelines-for-collections) – Ňɏssa Pøngjǣrdenlarp Jul 14 '18 at 17:21
  • @UnholySheep because I want them to have specific names accessible as both properties outside the class and also to be able to iterate through them in a loop to do things to them – jamheadart Jul 14 '18 at 17:36
  • So why not make them [properties](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties)? e.g.: `public item ITEM1 { get { return myItems[0]; } }`? – UnholySheep Jul 14 '18 at 17:37
  • By adding the `get` for each `item` with a different index, does that then mean I can then access it through `myItems[x]` ? – jamheadart Jul 14 '18 at 17:42
  • I tried this in a stand-alone project and referring to the collection[x] before it's added to the collection didn't work unless I'm missing something obvious it sorta defied the point of what I was hoping to achieve. – jamheadart Jul 14 '18 at 18:12

2 Answers2

1

Your second example won't work they way you think in 2 respects.

1) The ITEM1 and ITEM2 properties will not get assigned.

What's happening is that you're adding 2 x null to your List. Those nulls no longer hold any relation to ITEM1 nor ITEM2.

2) Those 2 new items will not get assigned to your List

What's happening is that the foreach operator passes a pointer to null into variable i, but your assignment then simply overwrites that pointer with another pointer to the new item. This would also happen if the List was holding anything other than null. You're overwriting the pointer. i then no longer has any relation to the List.

You seem to confusing pointers and references. If this were to all use references it'd work the way you assume: The references to ITEM1 and ITEM2 would get added to the List and they would get assigned a new item. Unfortunately (for you :p), that's not how it works.

Vincent Vancalbergh
  • 3,267
  • 2
  • 22
  • 25
1

You can use the for-statment, not foreach which requires the items to be in the list already

const int MaxCount = 20;

public List<item> myItems = new List<item>();

public item_holder()
{
    // Add 20 items to the list.
    for (int i = 0; i < MaxCount; i++) {
        myItems.Add(new item());
    }
}

The standard way of working with numbered variables

string s1;
string s2;
string s3;
// ...
string sN;

if to use an array instead and to index the items;

string[] s = new string[N];
s[0] = "hello";
s[1] = "world";
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Yup that makes sense completely but I actually wanted my `item` to have a specific instance name, so it's accessible by name which isn't possible this way, that's why I wanted to dimension them first. – jamheadart Jul 14 '18 at 17:34
  • If you want to access them by name, you can consider using a Dictionary, where you can store a key along with a value. – Vincent Vancalbergh Jul 14 '18 at 17:39
  • Yup that's also possible but I wanted the instance name to pop-up using intellisense rather than passing a string which could be misspelled - I considered using constants for the names but I thought things were getting convoluted. I keep hitting the same problem every time I attack it from a different angle! :D – jamheadart Jul 14 '18 at 17:44