用户提供“从”和“到”月份来生成报告(例如,从 1 个月前到 13 个月前;如果他们在 2016 年 2 月 15 日选择此选项,则返回值为 2015 年 1 月 1 日和2016 年 1 月 1 日)。
我想允许用户从“从”或“到”组合框中选择最远的月份或最近的月份。我只想将最远的时间用作“从”,将最接近的时间用作“到”,以避免对他们造成混淆(他们可以做任何他们认为自然的事情)。
所以我从这段代码开始:
int fromMonths = Convert.ToInt32(comboBoxProduceUsageFrom.Text);
DateTime RptParamsNominalFromDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(fromMonths, nextGenerateAndSendDate);
int toMonths = Convert.ToInt32(comboBoxProduceUsageTo.Text);
DateTime RptParamsNominalToDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(toMonths, nextGenerateAndSendDate);
..然后我想将“开始”日期设置为最远的时间,并将“到”日期设置为更近的时间。我首先尝试了这个:
DateTime RptParamsFromDate;
DateTime RptParamsToDate;
if (RptParamsNominalFromDate > RptParamsNominalToDate)
{
RptParamsFromDate = RptParamsNominalToDate;
RptParamsToDate = RptParamsNominalFromDate;
}
else if (RptParamsNominalToDate > RptParamsNominalFromDate)
{
RptParamsFromDate = RptParamsNominalFromDate;
RptParamsToDate = RptParamsNominalToDate;
}
...但这失败了,“使用未分配的局部变量'RptParamsFromDate'”(以及“RptParamsToDate ”的相同错误)。
所以我尝试给 DateTimes 一个值/非值,如下所示:
DateTime RptParamsFromDate = null;
DateTime RptParamsToDate = null;
...但这给了我,“无法将 null 转换为 'System.DateTime',因为它是不可为空的值类型”
所以我再次动了动手指并尝试将 DateTimes 设为空:
DateTime? RptParamsFromDate = null;
DateTime? RptParamsToDate = null;
...但后来我得到,“ 'System.Nullable'不包含'ToLongDateString'的定义,并且找不到接受'System.Nullable'类型的第一个参数的扩展方法'ToLongDateString'(您是否缺少使用指令还是程序集引用?) “
这是由于以下代码:
RptParamsFromDate.ToLongDateString()
在这个块中:
MessageBox.Show(string.Format(
"Using the current configuration, the Produce Usage report would next be sent on {0} and emailed to {1}; the report would cover data from {2} to {3}",
nextGenerateAndSendDate.ToLongDateString(),
emailRecipients,
RptParamsFromDate.ToLongDateString(),
RptParamsToDate.ToLongDateString()));
那么我能做些什么来显示 DateTime 值并仍然安抚脾气暴躁的野兽呢?
更新
结合来自 SLaks 和 crashmstr 的信息,我最终得到了以下工作方法:
private void buttonTestProdUsageSettings_Click(object sender, EventArgs e)
{
// Show example of when the report will run, and using which parameters,
// using the current configuration
DateTime nextGenerateAndSendDate = GetNextProduceUsageGenerateAndSendDate();
string emailRecipients = string.Join(",", emailAddresses.ToArray());
int fromMonths = Convert.ToInt32(comboBoxProduceUsageFrom.Text);
DateTime RptParamsNominalFromDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(fromMonths, nextGenerateAndSendDate);
int toMonths = Convert.ToInt32(comboBoxProduceUsageTo.Text);
DateTime RptParamsNominalToDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(toMonths, nextGenerateAndSendDate);
if (RptParamsNominalFromDate.Equals(RptParamsNominalToDate))
{
MessageBox.Show("The \"from\" and \"to\" values must differ; please try again.");
return;
}
// Allow user to enter either the nearest or furthest value in either the "from" or the "to":
DateTime? RptParamsFromDate = null;
DateTime? RptParamsToDate = null;
if (RptParamsNominalFromDate > RptParamsNominalToDate)
{
RptParamsFromDate = RptParamsNominalToDate;
RptParamsToDate = RptParamsNominalFromDate;
}
else if (RptParamsNominalToDate > RptParamsNominalFromDate)
{
RptParamsFromDate = RptParamsNominalFromDate;
RptParamsToDate = RptParamsNominalToDate;
}
MessageBox.Show(string.Format(
"Using the current configuration, the Produce Usage report would next be sent on {0} and emailed to {1}; the report would cover data from {2} to {3}",
nextGenerateAndSendDate.ToLongDateString(),
emailRecipients,
RptParamsFromDate.HasValue ? RptParamsFromDate.Value.ToLongDateString() : "No \"from\" Date",
RptParamsToDate.HasValue ? RptParamsToDate.Value.ToLongDateString() : "No \"to\" Date"));
}