![]() |
| | #1 |
| eh ya hoser, got a beer? Join Date: Feb 2003
Posts: 287
| Creating instances of objects in an array? 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();
}
}
}
(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 | |
| | #2 |
| Registered User Join Date: Jun 2008 Location: RING 0
Posts: 462
| Keep Code: Cars[] driver = new Cars[RACERS]; |
| valaris is offline | |
| | #3 |
| Confused 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 | |
| | #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 | |
| | #5 |
| eh ya hoser, got a beer? Join Date: Feb 2003
Posts: 287
| Thank you guys.
__________________ The keyboard is the standard device used to cause computer errors! |
| stumon is offline | |
| | #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)
};
...
}
}
|
| arpsmack is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |