本文介绍了DateTime在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下课程
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; } } }当我调试并在DateTime.Now.Date上使用添加手表"时,我得到了
When I'm debugging and i use "Add watch" on DateTime.Now.Date, i get
"DateTime在当前上下文中不存在."
"DateTime does not exist in the current context."
即使日期相同,条件也永远不会得到满足.
And the condition is never satisfied even if the dates are the same.
为什么我要包含系统?
推荐答案以下是我看到的一些内容;
Here a few things I saw;
- 您可能需要在项目中添加 mscorlib.dll 作为参考.
- 您不能与 DateTime ( newDate )和 int ( DateTime.Now.Day )进行比较使用 == 运算符.您可能需要改用 newDate == DateTime.Now .
- return 语句后,您缺少分号(; ).
- 您需要添加 System.Globalization 名称空间,用于使用 CultureInfo 类.
- 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.
更多推荐
DateTime在当前上下文中不存在
发布评论