mercredi 28 janvier 2015

Consecutive object creation using it's predecessors data

I currently have a school assignment, mainly focusing on Object Oriented Programming & using Collections.


I know how most of this is done, however I seem to have hit a snag: I coded my program so it makes 10 objects, puts them in a List<T> & then outputs them to a ListBox.


Which works fine, but for some reason, every single object is receiving the same exact values, even though they should be (pseudo)random.


Here's the object's class (named Boom), including it's constructor:



public class Boom
{
public static int aantalBomen = 0;
public enum boomType { Naald, Loof }

public boomType BoomType { get; set; }
public int VolgNr { get; set; }

private int aantalVogels;
public int AantalVogels
{
get { return aantalVogels; }
set
{
if (value >= 0 && value <= 10)
aantalVogels = value;
}
}

public Eekhoorn Eekhoorn { get; set; }

private int hoogte;
public int Hoogte
{
get { return hoogte; }
set
{
if (value >= 5 && value <= 10)
hoogte = value;
}
}

public bool eekHoorn = false;

public Boom()
{
aantalBomen++;
VolgNr = aantalBomen;
Random rnd = new Random();
Hoogte = rnd.Next(5, 11);
AantalVogels = rnd.Next(0, 11);
BoomType = (boomType)Enum.GetValues(typeof(boomType)).GetValue(rnd.Next(0, Enum.GetValues(typeof(boomType)).Length));
}

public void MaakEekhoorn()
{
Eekhoorn = new Eekhoorn();
eekHoorn = true;
aantalVogels = (int)Math.Round((double)(aantalVogels / 2), 0);
}
}


& Here's the loop which creates the 10 objects:



public List<Boom> Bomen = new List<Boom>();
for (int i = 1; i <= 10;i++ )
{
Bomen.Add(new Boom());
}


Finally, the code I'm using to test the creation of the objects:



for (int i = 0;i<10;i++)
{
lstBomen.Items.Add("VolgNr: " + bos.Bomen[i].VolgNr.ToString() + " AantalVogels: " + bos.Bomen[i].AantalVogels.ToString() + " BoomType: " +
bos.Bomen[i].BoomType.ToString() + " Hoogte: " + bos.Bomen[i].Hoogte.ToString());
}


I can't seem to figure out why my code keeps generating the exact same values for all 10 objects, resulting in output like this:


Output


If you need any more info, pertaining to code or anything connected to the program's execution, feel free to ask.





Aucun commentaire:

Enregistrer un commentaire