作者:手机用户2502893535 | 来源:互联网 | 2023-02-09 15:17
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
我正在尝试使用一种可以通过表单访问的服务中的方法,以便我可以使用此方法在日期时间选择器中生成随机日期。它只是不起作用,我有两个DTP称为dtp_Current和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()
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
是我想要在服务中调用的方法,我将如何执行此操作,因此当单击后按钮时,它会检查dtp_Current日期以查看dtp_New是否更大
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
摘要:有一个表单和一个服务引用需要服务引用在dtp_Current中生成一个随机日期然后当点击之前或之后在dtp_New中生成一个新日期然后检查dtp_New是否大于或小于dtp_Current
3 个解决方案
0
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).
(注意:首先生成月份,然后查找结果月份中的天数,并在生成日期数字时将其用作上限,因此2月份最多可获得28或29,等等)。
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.
创建日期后,您只需使用<=和> =(有或没有=)比较它们。如果需要在服务上下文中对它们进行比较,只需将DateTime作为参数发送给服务,并在那里进行比较。
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).
在客户端的Visual Studio项目下打开该文件夹。右键单击服务引用,然后选择“在对象浏览器中查看”。在那里,您应该看到服务中的类型和命名空间的层次结构。查找名为YourServiceNameClient的类型。这将是一个自动生成的类型,可用于连接到您的服务(客户端将仅附加到服务引用已标识的类型)。
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...
这显然假设GetNewRandomDate()是您在服务中公开的方法。希望这有用......