I am attempting to create an array of objects of type "Creature",
so I decalre an array of type Creature as following:
Creature[] creatures = new Creature[50];
and then use a "for" statement to step through the array and fill it with new elements of type Creature ex.:
for (int i = 0; i < creatures.Length; i++)
{
creatures[i] = new Creature();
}
the problem is that instead of each "Creature" in the array being a different object they all seem to be a reference to the same object, but when I set a breakpoint on the "creatures[i] = new Creature();" line of the for loop and manually step through the code, then it works correctly and each object is different (I get differing behavior in debug mode). Anyone have a take on this? (not really expecting a solution here)
2006-10-02
09:34:20
·
4 answers
·
asked by
Nick F
6
in
Computers & Internet
➔ Programming & Design
William, my default constructor for each "creature" gets a random number and converts into a color of type "Color", if I step through manually it works fine (the color of each creature is a random color) but when I set no breakpoint the color (and other private variables) are all copies of the first object in the array, they all seem of incorrectly refer to the same object instead of being references to seperate objects.
2006-10-02
09:59:16 ·
update #1
cd4017, that must be it, since I am using a random number in my constructor, not enough time is elapsing, thank you very much
2006-10-02
10:02:04 ·
update #2