Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 08, 2012 07:29 PM by Paul Linton
May 08, 2012 04:32 PM|LINK
I have a class i'm going to use for mock databinding to a grid while my team members are still working on the DAL/BLL layers. I was using random to create different mock data within the getters and setters. Everything works great until I add a property that
is of type DateTime. As soon as I add that property all of the data is the same regardless of what I pass into the seed value of random constructor. Any ideas?
May 08, 2012 04:40 PM|LINK
http://www.alexandre-gomes.com/?p=128 "How To: Random DateTime(s)"
i've not tested this; however, it looks like it should work.
May 08, 2012 05:53 PM|LINK
Thanks. Actually I don't even care if the datetime is random or not. I did at first but really I just want the other data to be random. As soon as I add the datetime property to the class it affects the other properties which contain random in them.
private string _firstName = string.Empty;
public string FirstName
Random rnd = new Random();
if (rnd.NextDouble() < 0.5)
this._firstName = value;
private DateTime _myDate;
public DateTime MyDate
this._myDate = value;
May 08, 2012 07:23 PM|LINK
I'm not sure how your DateTime property caused this issue, but having a non-random values being created when you actually expect, well, randow values, is a known bad approach of using your Random object.
The Random objects are created using the clock. If you create multiple Random(), chances are, they are going to return the same output when calling Next().
The fix is to use a single Random() object in your application, or in the scope you require, and just call Next() on that same object.
May 08, 2012 07:29 PM|LINK
The reason you get the same result every time is because you always return DateTime.Today. You could do it like you did for FirstName, generate a random number and choose one of two dates based on the number generated.
But ... this seems like a crazy difficult way to design time data. Just hardcode a bunch of information and then bind the collection to the data source for the grid. Just type it in, don't mess around with random and getters and setters.