Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am trying to create (MAIN) objects dynamically and these objects are then going to have a list of dynamic (SUB) objects. My problem is that I don't know how to create X amount of sub objects (bombs) for 1 main object. At least as comment suggests I have an idea but it doesn't work and I don't see any other way of doing it. Please help me. Any comments or suggestions are welcome.

C#
class Program
    {
        static void Main(string[] args)
        {
            // T = number of test cases
            // N = number of bombs
            int T = 0, N = 0;
            int[] space3D = new int[3];
            List<Testcase> testCaseList = new List<Testcase>();
            List<Bomb> bombs = new List<Bomb>();

            Random rnd = new Random();

            // open file
            StreamReader reader = File.OpenText("temp.txt");

            // read file
            try
            {
                T = Convert.ToInt32(reader.ReadLine());
                N = Convert.ToInt32(reader.ReadLine());
            }
            catch(Exception error)
            {
                reader.Close();
                Console.WriteLine("Invalid input : {0}", error);
            }

            // display
            Console.WriteLine("The number of test cases = {0}", T);
            Console.WriteLine("The number of bombs = {0}", N);

            // close file
            reader.Close();

            // create testcases
            while(T != 0)
            {
                testCaseList.Add(new Testcase(T, new List<Bomb>()
                {
                    //while(N != 0)
                    //{
                        new Bomb(rnd.Next(0, 1000), rnd.Next(0, 1000), rnd.Next(0, 1000)),
                    //}
                }));
                T--;
            }

            // print all testcases
            foreach (var testcase in testCaseList)
            {
                testcase.print();
            }


            Console.Read();
        }

        private static void printTitle()
        {
            Console.WriteLine("Exercise 2 - Safest place in the galaxy");
            Console.WriteLine("=======================================");
        }
    }




If you need it here is my constructor :

C#
class Testcase
    {
        int numberOfBombs { get; set; }
        //{
        //    get { return N; }
        //    set { if ((N >= 1) && (N <= 200)) { N = value; } }
        //}

        List<bomb> bombList { get; set; }

        public Testcase() { }

        public Testcase(int _noOfBombs, IEnumerable<bomb> _bombs)
        {
            numberOfBombs = _noOfBombs;
            bombList = new List<bomb>(_bombs);
        }

        public void print()
        {
            Console.WriteLine("Test case number {0} initialized.", numberOfBombs);
        }
    }
Posted
Updated 1-Oct-13 14:11pm
v4
Comments
Sergey Alexandrovich Kryukov 1-Oct-13 17:48pm    
"It won't let me"?! Then show the code which someone or something prevented from working. What is was? All my guesswork leads me nowhere. :-)
If you need a loop, write a loop.
—SA
dedo1991 1-Oct-13 18:06pm    
I am sorry. Maybe I am just too tired but I don't get your answer. Not the one above but the one below in solution section. Could you provide a little bit of code to show me what you meant please?
Sergey Alexandrovich Kryukov 1-Oct-13 18:42pm    
Is my answer also unclear? Just follow the logic: one constructor call, one "Add" to the list.
—SA

Turn this initialization inside-out:
C#
// create testcases
while(T > 0)  // if this was != what happens if T starts negative?
{
  List<Bomb> bombs = new List<Bomb>();
  while (N-- > 0)  // if this was != what happens if N starts negative?
  {
    bombs.Add(new Bomb(rnd.Next(0, 1000), rnd.Next(0, 1000), rnd.Next(0, 1000)));
  }

  testCaseList.Add(new Testcase(T, bombs));
  T--;
}

Also, why not have the Testcase constructor take List<Bomb> instead of IEnumerable<Bomb>? It is pointlessly creating a new List.
In Testcase you're using numberOfBombs as the Test case number. Since that field is NOT actually the number of Bombs, then rename it as what it really is!

Another possibility is to have Testcase's constructor take the Test case number as the only parameter, always initialize the bombList to an empty List<Bomb> and then have an AddBomb() method that takes an instance of Bomb as its parameter. Then the initialization while loop will create an instance of Testcase (keep a local reference to it!), create N Bomb instances passing them to the reference_to_Testcase.AddBomb() method as they are created, testCaseList.Add(reference_to_Testcase) to collect them.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Oct-13 20:19pm    
Nice, 5ed.
—SA
Matt T Heffron 1-Oct-13 20:27pm    
Thanks.
Isn't it obvious? If you want to create a loop with new Bomb and create count bombs, you also need to call it to the list count times. You are creating instanced of a Bomb, but those instances become unreachable at the end of your '{...}' scope, so eventually they are destroyed by the Garbage Collector (GC). And this is good: make love, not war!

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900