Saturday, August 3, 2013

Object Initialization problem / issue in Visual Studio 2012?

David is correct - you can fix this (provided you're not using multithreading) by making the Random instance static:



class ItemClass
{
private static Random r = new Random(); //Share one instance across all ItemClass instances

private int id { get; set; }
public double[] ws { get; set; }

public ItemClass(int identifier)
{
id = identifier;
ws = new double[10];
for (int z = 0; z < 10; z++)
{
ws[z] = r.NextDouble(); // Use the static instance
}
}
}





Reed Copsey, Jr. - http://reedcopsey.com - If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.


No comments:

Post a Comment