用户提供"来自"和"到"生成报告的月份(例如,从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'因为它是一个不可为空的值类型"
所以我再次动手并尝试将日期时间弄清楚:
DateTime? RptParamsFromDate = null;
DateTime? RptParamsToDate = null;
...但是我得到了" ' System.Nullable'不包含' ToLongDateString'的定义没有扩展方法' ToLongDateString'接受类型为#System; Nullable'的第一个参数。可以找到(你错过了使用指令或汇编引用吗?)"
这是由于这段代码:
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"));
}
答案 0 :(得分:5)
您正在尝试使用可空类型的值。
为此,您需要访问其IllegalStateException
属性,该属性返回常规.Value
值。
请注意,如果它实际上为null,则会抛出异常。
答案 1 :(得分:1)
要添加Slaks的原始答案,您获得第一个答案的原因是因为您必须在代码中稍后尝试引用RptParamsFromDate
或其亲属。问题是因为:
您已完成:
RptParamsNominalFromDate == RptParamsNominalToDate
)使用未分配的本地变量' RptParamsFromDate'" (" RptParamsToDate
的错误相同将其设置为DateTime?
会稍微超过一点,但您需要首先查看逻辑问题。在您稍后尝试使用它之前,请确保您已检查此为空。