c# - 运算符 '>='不能应用于 'System.DateTime?'和 'int'类型的操作数

标签 c# compiler-errors

我的代码是这样的,但是我需要验证RemainingDay是否大于10,然后在剩余的几天中不会显示消息框

if (Convert.ToDateTime(dr[0]) == DateTime.Today)
{
    MessageBox.Show("Your trail period is expired. Please purchase this Software.", "SMS");
    Application.Exit();
}
else
{
    newDate = Convert.ToDateTime(dr[0]);
    RemainingDay = newDate.Subtract(TodayDate);

    MessageBox.Show("You have " + RemainingDay.Days + " days Left.", "SMS");
    this.Hide();

    frmMainMenu frm = new frmMainMenu();
    frm.Show();
    frm.lblUser.Text = txtUserName.Text;
}

我尝试了下面的代码:
if (Convert.ToDateTime(dr[0]) == DateTime.Today)
{
    MessageBox.Show("Your trail period is expired. Please purchase this Software.", "SMS");
    Application.Exit();
}
else
{
    newDate = Convert.ToDateTime(dr[0]);
    RemainingDay = newDate.Subtract(TodayDate);

    if (RemainingDay <= 10)
    {
        MessageBox.Show("You have " + RemainingDay.Days + " days Left.", "SMS");
        this.Hide();
    }
    else
    {
        this.Hide();
        frmMainMenu frm = new frmMainMenu();
        frm.Show();
        frm.lblUser.Text = txtUserName.Text;
    }                        
}

上面的代码给我这个错误:

Operator '>=' cannot be applied to operands of type 'System.DateTime?' and 'int'



请帮我。

最佳答案

您的RemainingDay变量是 TimeSpan ,它是给定的DateTime和今天的日期之间的差。您看不到TimeSpan是否小于数字-这没有任何意义。

您想要得到的是日期之间的天数差,TimeSpan保存在 .Days 属性中,这将给您(来自MSDN)

the days component of the time interval represented by the current TimeSpan structure.



所以用那个代替。

关于c# - 运算符 '>='不能应用于 'System.DateTime?'和 'int'类型的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23809976/

相关文章:

scala - 尝试调试时总是获取ClassNotFoundException

java - 为什么这个通用代码在 java 8 中编译?

kotlin getparameters函数不起作用

c# - 在 appSettings 中为一个键使用多个值

c# - 根据绑定(bind)值更改元素属性

java - 这个深奥的泛型错误是编译器错误还是新限制? (推断类型不符合上限)

java - 需要添加一个 add 方法,但每次都会收到不同的错误,我不知道其含义

c# - 由于 System.Security.SecurityException,Winform 应用程序无法启动

c# - 麦克风停止向 NAudio 提供数据

c# - Datagridview 不显示数据