C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-19-2009, 06:54 PM   #1
eh ya hoser, got a beer?
 
stumon's Avatar
 
Join Date: Feb 2003
Posts: 287
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!
stumon is offline   Reply With Quote
Old 10-20-2009, 07:15 AM   #2
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 462
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.
valaris is offline   Reply With Quote
Old 10-20-2009, 07:39 AM   #3
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Old 10-20-2009, 08:45 AM   #4
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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();
        }
    }
}
theoobe is offline   Reply With Quote
Old 10-20-2009, 08:28 PM   #5
eh ya hoser, got a beer?
 
stumon's Avatar
 
Join Date: Feb 2003
Posts: 287
Thank you guys.
__________________
The keyboard is the standard device used to cause computer errors!
stumon is offline   Reply With Quote
Old 10-27-2009, 07:37 PM   #6
Registered User
 
Join Date: Jan 2008
Posts: 276
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
arpsmack is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a Spreadsheet using a multidimensional array Apiatan C++ Programming 1 03-21-2009 04:18 PM
How do I clean up a dynamic array of pointers to objects? rakan C++ Programming 4 11-02-2006 11:41 AM
How do I make a function that accepts an array of objects as it's argument rakan C++ Programming 11 09-22-2006 05:21 PM
Creating array from data file... Crankit211 C Programming 2 12-06-2001 09:45 PM
Creating an array of object pointers Unregistered C++ Programming 2 10-08-2001 10:01 PM


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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