DateTime does not exist in the current context

被刻印的时光 ゝ 提交于 2021-02-16 16:19:07

问题


I have the following class

using System;
using System.Globalization;

namespace GenericSomething
{
    public class Specific : Generic
    {
        public override bool DoSomething(string date)
        {
            DateTime newDate = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);
            if (newDate.Date == DateTime.Now.Date)
                return true;
            else
                return false;
        } 
    }
}

When I'm debugging and i use "Add watch" on DateTime.Now.Date, i get

"DateTime does not exist in the current context."

And the condition is never satisfied even if the dates are the same.

Why is that is I'm including System?


回答1:


Here a few things I saw;

  • You might be need to add mscorlib.dll as a reference in your project.
  • You can't compare with DateTime (newDate) and int (DateTime.Now.Day) with == operator. You might need to use newDate == DateTime.Now instead.
  • You are missing semi-colon (;) after your return statements.
  • You need to add System.Globalization namespace for using CultureInfo class.



回答2:


Also, check to make sure that you have typed in "DateTime" and not "Datetime" or something else. It has to be "DateTime" (camel case).



来源:https://stackoverflow.com/questions/21526584/datetime-does-not-exist-in-the-current-context

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!