0

I'm working on a project where I need to create many objects. I would like to call the objects player1, player2, player3, etc.

Class Player
{
   public string Name { get; set; }
   public List<string> NotPlayedAgainst { get; set; }
   public List<string> NotPlayedWith { get; set; }
   public int PositivePoints { get; set; }
   public int NegativePoints { get; set; }
}

Now creating the objects by repeating following code:

int x = 1;
Player player(x) = new Player();
x++;

I need to be able to call the objects using a rule later in the code. So I would also like to know how to call these objects using a rule like this:

player(x).Name = /*some code*/
  • 5
    There is no reason to do that. Use a generic `List` instead and new instances to that list. You will also need to use a `for` loop. – Igor May 07 '18 at 13:01
  • You can even use a dictionary, give your objects proper names, and enumerate over the keys. – Stefan May 07 '18 at 13:03
  • Generic list, generic dictionary, even a simple array. Any time you think you want a dynamic variable name, what you *probably* want is a collection. – David May 07 '18 at 13:04
  • 1
    As a side note do not name your class `Object` – Igor May 07 '18 at 13:04
  • 2
    This is a X-Y problem. Tell us what you really want to do – adjan May 07 '18 at 13:07
  • maybe you can also find answers here https://stackoverflow.com/questions/4658726/c-dynamic-object-names – Nerevar May 07 '18 at 14:25
  • You should post a new question rather than editing this closed question. A new question will get you more visibility and a better chance of an answer. – caffeinated.tech May 08 '18 at 13:06

3 Answers3

2

Either add these objects to a List<Object> and access them via index or (depending on your real requirement) provide a property to identify the object, for example Id:

class Object
{
   public Object(int id)
   {
      this.Id = id;
   }

   public int Id { get;}
}

or both:

List<Object> objectList = new List<Object>();
for(int id = 1; id <= 10; id++)
{
    Object objectX = new Object(id);
    objectList.Add(objectX);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

You have many ways...

List approach:

class Program
{
    static void Main(string[] args)
    {
        List<Dog> dogs = new List<Dog>();
        for(int i=0; i < 100; i++)
        {
            dogs.Add(new Dog());
        }
        Dog firstDog = dogs[0];
    }
}

class Dog
{

}

Array approach:

class Program
{
    static void Main(string[] args)
    {
        Dog[] dogs = new Dog[100];
        for(int i=0; i < 100; i++)
        {
            dogs[i] = new Dog();
        }
        Dog firstDog = dogs[0];
    }
}

class Dog
{

}

Dictionary approach:

class Program
{
    static void Main(string[] args)
    {
        Dictionary<int, Dog> dogs = new Dictionary<int, Dog>();
        for(int i=0; i < 100; i++)
        {
            dogs.Add(i, new Dog());
        }
        Dog firstDog = dogs[0];
    }
}

class Dog
{

}
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

Use Dictionary<key, value>:

Dictionary<string, object> objects = new Dictionary<string, object>();

            for (int i = 0; i < 5; i++)
            {
                objects.Add("obj" + i, new object());
            }

and to access the object of index 2:

object myObj = objects["obj2"];
mshwf
  • 7,009
  • 12
  • 59
  • 133