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)
Update:cd4017, that must be it, since I am using a random number in my constructor, not enough time is elapsing, thank you very much
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
Verified answer
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; }
}
}
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"?
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/Workin...
If it still gives your problems then email me your code and I will sort it out for you
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.