我正在尝试将值写入 CurrentRent.Text 和 NextRent.Text (我的表单上的两个标签并继续收到以下错误消息:
Error 1 'System.DateTime' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)
Error 2 'System.DateTime' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)
我该如何解决?到目前为止,这是我的代码:
public Form1()
{
InitializeComponent();
CurrentDate.Text = "Today's Date: " + DateTime.Now;
DateTime CurrentRent;
DateTime NextRent;
DateTime today = DateTime.Now;
if (today.DayOfWeek == DayOfWeek.Wednesday)
{
CurrentRent.Text = "Current Rent Date: " + today.AddDays(-7);
NextRent.Text = "Next Rent Date: "+ today.AddDays(7);
}
}
答案 0 :(得分:3)
嗯,这个错误对我来说非常清楚。您正在尝试设置DateTime
的文本。 DateTime
没有Text
属性。如果要设置某个标签的文本,请设置标签的文本,而不是DateTime
的文本。
答案 1 :(得分:1)
您声明了一个“隐藏”标签的本地变量:
DateTime CurrentRent;
只是评论出来:
// Don't include this - it hides your label:
// DateTime CurrentRent;
由于这是在本地声明的,因此您尝试设置本地CurrentRent变量的“Text”属性,而不是您的标签。