English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

4 answers

With the code below, if you run it all the creatures are identical and if you step by step then the creatures are different. Is this your problem?? (explanation is below)

using System;
using System.Collections.Generic;
using System.Text;

namespace test1
{
class Creature
{

private int i;
public Creature()
{
Random rnd = new Random();
i = rnd.Next();
}
public int value
{
get { return i; }
}
}
class Program
{
static void Main(string[] args)
{
Creature[] creatures = new Creature[50];

for (int i = 0; i < creatures.Length; i++)
{
creatures[i] = new Creature();
}
for (int i = 0; i < creatures.Length; i++)
{
Console.WriteLine(creatures[i].value);
}
}
}
}

The problem comes from the line
Random rnd = new Random();
This line initialize the random generator with a seed from the PC timer. If you run at full speed (without debug), the seed is always the same and all your creatures are intialized with EXACTLY the same random sequence.

To solve it you can use an external random variable that is initalized only once.

Example:

class Creature
{
static Random rnd = new Random(); /note the static!
private int i;
public Creature()
{
i = rnd.Next();
}
public int value
{
get { return i; }
}
}

2006-10-02 09:57:10 · answer #1 · answered by cd4017 4 · 1 0

Perhaps the problem is in the definition of the Creature object. Does it have any internal storage?

Also, what data do you base your statement on "they all seem to be a reference to the same object"?

2006-10-02 16:52:02 · answer #2 · answered by DadOnline 6 · 0 0

Try to use a foreach loop rather than a for loop and see if that makes a difference. The link below might be of help :

http://www.c-sharpcorner.com/Code/2002/July/WorkingWithArrays.asp

If it still gives your problems then email me your code and I will sort it out for you

2006-10-02 17:10:06 · answer #3 · answered by Anonymous · 0 0

I must admit that I became rather excited upon seeing the subject header question. I thought I might be able to help since I thought this was a musical question. Imagine my surprise to discover that this is actually a computer question. lol I should have checked the category first.

Now that I see it, I can only say thank you for giving me a good laugh at myself & good luck on getting solid answers for your difficulty.

2006-10-02 16:44:25 · answer #4 · answered by Shadow 7 · 0 1

fedest.com, questions and answers