Creating instances of objects in an array?

This is a discussion on Creating instances of objects in an array? within the C# Programming forums, part of the General Programming Boards category; I have a Cars class already created and using a form, I am trying to create an array of classes ...

  1. #1
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323

    Creating instances of objects in an array?

    I have a Cars class already created and using a form, I am trying to create an array of classes and set each member of the array with an instance of the class. I am trying to do this within the public partial class form1 : form that Visual studio creates. I am getting an error when I try to create an instance of the class after I create the array. here is the code and the errors I am getting.

    Code:
    namespace VirtualRace
    {
        public partial class Form1 : Form
        {
            const int RACERS = 10;
            Cars[] driver = new Cars[RACERS];
            driver[0] = new Cars("Andretti", 30, 0);
            driver[1] = new Cars("Patrick", 30, 0);
            driver[2] = new Cars("Elvers", 30, 0);
            driver[3] = new Cars("Smith", 30, 0);
            driver[4] = new Cars("Archibald", 30, 0);
            driver[5] = new Cars("Law", 30, 0);
            driver[6] = new Cars("Johnson", 30, 0);
            driver[7] = new Cars("Petty", 30, 0);
            driver[8] = new Cars("Freeman", 30, 0);
            driver[9] = new Cars("Busch", 30, 0);
            private int raceLength = 1000;
            private bool winner = false;
            private int leadLocation = 0;
            private int leadDriver = 0;
            
            public Form1()
            {
                InitializeComponent();
            }
        }
    }
    And the errors I am getting are: (there are these 4 errors per line starting on the driver[0] line and going till driver[9])

    (16,16): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
    (16,19): error CS1519: Invalid token '=' in class, struct, or interface member declaration
    (16,25): error CS1520: Method must have a return type
    (16,30): error CS1031: Type expected


    Basically I am wondering how I can fill this array with these objects and keep them global to this class so I can use them in any Method I create? The code seems to work if I create the array and create each member within a single method, but not when I try it like this to make it available to the whole class. Any help is appreciated.
    The keyboard is the standard device used to cause computer errors!

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Keep
    Code:
    Cars[] driver = new Cars[RACERS];
    at class scope, and initialize/create each object in the constructor. If you need to use these objects as early as the constructor then just construct them first thing during the constructor.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Use a list collection, and you don't have to know the size beforehand:
    Code:
    var driver = new System.Collections.Generic.List<Cars>();
    driver.Add(new Cars("Andretti", 30, 0));
    driver.Add(new Cars("Patrick", 30, 0));
    driver.Add(new Cars("Elvers", 30, 0));
    ...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    You are not allowed to set your array's element values at class level, it must be done from within a method.

    Code:
    namespace VirtualRace
    {
        public partial class Form1 : Form
        {
            const int RACERS = 10;
            private Cars[] driver = new Cars[RACERS];
            private int raceLength = 1000;
            private bool winner = false;
            private int leadLocation = 0;
            private int leadDriver = 0;
            
            public Form1()
            {
                driver[0] = new Cars("Andretti", 30, 0);
                driver[1] = new Cars("Patrick", 30, 0);
                driver[2] = new Cars("Elvers", 30, 0);
                driver[3] = new Cars("Smith", 30, 0);
                driver[4] = new Cars("Archibald", 30, 0);
                driver[5] = new Cars("Law", 30, 0);
                driver[6] = new Cars("Johnson", 30, 0);
                driver[7] = new Cars("Petty", 30, 0);
                driver[8] = new Cars("Freeman", 30, 0);
                driver[9] = new Cars("Busch", 30, 0);
    
                InitializeComponent();
            }
        }
    }

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Thank you guys.
    The keyboard is the standard device used to cause computer errors!

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Well, the OP probably doesn't care anymore, but I feel I should mention that you can initialize an array without using a constructor.

    Code:
    namespace VirtualRace
    {
       public partial class Form1 : Form
       {
          Cars[] driver = new Cars[]
          {
             new Cars("Andretti", 30, 0),
             new Cars("Patrick", 30, 0),
             new Cars("Elvers", 30, 0),
             new Cars("Smith", 30, 0),
             new Cars("Archibald", 30, 0),
             new Cars("Law", 30, 0),
             new Cars("Johnson", 30, 0),
             new Cars("Petty", 30, 0),
             new Cars("Freeman", 30, 0),
             new Cars("Busch", 30, 0)
          };
          ...
       }
    }
    As a bonus for using this approach, you no longer have to maintain a constant for the number of racers, just use driver.Length

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Spreadsheet using a multidimensional array
    By Apiatan in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2009, 05:18 PM
  2. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  3. Replies: 11
    Last Post: 09-22-2006, 06:21 PM
  4. Creating array from data file...
    By Crankit211 in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 09:45 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 11:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21