Question

I'm trying to have a kind of method that is in a service that can be accessed by a form so that I can use this method to generate a random date in a date time picker. It just doesn't work however, I have two DTP's called dtp_Current and dtp_New

This is accessed on a form and I have two buttons that say before and after, when before is clicked you are guessing the newly generated date will be before the current date and if you click after it's guessing it's gonna be after the currently generated date. I have to do this using a service however

public int RandomDate()

is what i'd like the method to be called in the Service, how would I go about doing this so when the after button is clicked it checks dtp_Current date to see if dtp_New is larger

I hope this makes sense

summary: have a form and a service reference need service reference to generate a random date in dtp_Current then when before or after is clicked generate a new date in dtp_New then to check if dtp_New is larger or smaller than dtp_Current

Was it helpful?

Solution 2

It seems to me like the main problem here is simply to create a method that generates a random date.

One way to do this and retain a certain level of control over which dates you get, is to just generate random numbers for date, month and year. For year, you could do:

Random r = new Random();

int randomYear = r.Next(1990, 2015); // random year between 1990 and 2014    
int randomMonthNr = r.Next(1,13);
int maxDayNr = DateTime.DaysInMonth(randomYear, randomMonthNr);
int randomDayNr = r.Next(1, (maxDayNr + 1));

Do something similar for date and month, and just use that in:

var randomDate = new DateTime(randomYear, randomMonthNr, randomDayNr);

(Note: Generate the month first, then find out the number of days in the resulting month, and use that as the upper limit when generating a number for the date, so you get a max of 28 or 29 for February, etc).

Once the date(s) are created, you can simply compare them using <= and >= (with or without the =). If you need to compare them within the context of the service, just send a DateTime as a parameter to the service, and compare it there.


Update: Connecting to the service

Open the folder under your Visual Studio project for the client. Right click the service reference, and select View in Object Browser. There, you should see a hierarchy of the types and namespaces from your service. Look for a type called something like YourServiceNameClient. This will be an automatically generated type that you can use to connect to your service (Client will just be appended to the type that the service reference has identified).

Use it like:

var yourServiceReference = new YourServiceNameClient();
var yourGeneratedDate = yourServiceReference.GetNewRandomDate();

This obviously assumes GetNewRandomDate() is a method you have exposed in your service. Hope this is helpful...

OTHER TIPS

To Generate the random date here is the c# code:

 DateTime RandomDay()
    {
        DateTime start = new DateTime(1900, 1, 1);
        Random gen = new Random();

        int range = (DateTime.Today - start).Days;           
        return start.AddDays(gen.Next(range));
    }

You can use random.Next using time in milliseconds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top