0

用户提供“从”和“到”月份来生成报告(例如,从 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"));
}
4

2 回答 2

5

您正在尝试使用可为空类型的值。

为此,您需要访问它的.Value属性,该属性返回一个常规DateTime值。

请注意,如果它实际上为空,则会引发异常。

于 2016-02-15T18:38:52.573 回答
1

要添加到 Slaks 的原始答案,您获得第一个答案的原因是因为您必须RptParamsFromDate在代码后面尝试引用或其亲属。问题是因为这个:

你已经完成了:

  1. 创建变量
  2. 是真的吗?不?...好的
  3. 还有什么是真的吗?不?...好的
  4. 变量仍未设置为任何内容。(因为RptParamsNominalFromDate == RptParamsNominalToDate
  5. (假设)您已尝试访问该变量。

使用未分配的局部变量“RptParamsFromDate””(以及“RptParamsToDate”的相同错误

将其设置为 aDateTime?将在某种程度上超越这一点,但您需要首先研究逻辑问题。确保在稍后尝试使用它之前检查它是否为空。

于 2016-02-15T18:43:12.997 回答