B. *_*non 0 c# datetime nullable unassigned-variable
用户提供"从"和"到"月份以生成报告(例如,从1个月回到13个月;如果他们在2016年2月15日选择此项,则返回值为2015年1月1日, 2016年1月1日).
我想允许用户从"from"或"to"组合框中选择最远的后面或最近的月份.我只想用最远的时间作为"从"和最接近的时间作为"to"以避免他们的混淆(他们可以做任何看起来很自然的事情).
所以我从这段代码开始:
int fromMonths = Convert.ToInt32(comboBoxProduceUsageFrom.Text);
DateTime RptParamsNominalFromDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(fromMonths, nextGenerateAndSendDate);
int toMonths = Convert.ToInt32(comboBoxProduceUsageTo.Text);
DateTime RptParamsNominalToDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(toMonths, nextGenerateAndSendDate);
Run Code Online (Sandbox Code Playgroud)
..然后我想将"从"日期设置为最晚的时间,并将"到"设置为更接近时间.我第一次尝试这个:
DateTime RptParamsFromDate;
DateTime RptParamsToDate;
if (RptParamsNominalFromDate > RptParamsNominalToDate)
{
RptParamsFromDate = RptParamsNominalToDate;
RptParamsToDate = RptParamsNominalFromDate;
}
else if (RptParamsNominalToDate > RptParamsNominalFromDate)
{
RptParamsFromDate = RptParamsNominalFromDate;
RptParamsToDate = RptParamsNominalToDate;
}
Run Code Online (Sandbox Code Playgroud)
...但是失败了," 使用未分配的局部变量'RptParamsFromDate'"(和"RptParamsToDate "的错误相同).
所以我尝试给DateTimes一个值/非值,如下所示:
DateTime RptParamsFromDate = null;
DateTime RptParamsToDate = null;
Run Code Online (Sandbox Code Playgroud)
...但是这给了我," 无法将null转换为'System.DateTime',因为它是一个不可为空的值类型 "
所以我再次设置动作并尝试对DateTimes进行nullablizing:
DateTime? RptParamsFromDate = null;
DateTime? RptParamsToDate = null;
Run Code Online (Sandbox Code Playgroud)
...但后来我得到," 'System.Nullable'不包含'ToLongDateString'的定义,并且没有扩展方法'ToLongDateString'接受类型'System.Nullable'的第一个参数'(你是否错过了使用指令或汇编参考?) "
这是由于这段代码:
RptParamsFromDate.ToLongDateString()
Run Code Online (Sandbox Code Playgroud)
在这个块中:
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()));
Run Code Online (Sandbox Code Playgroud)
那么我该怎么做才能显示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"));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
248 次 |
| 最近记录: |