'System.DateTime'不包含'Text'的定义

时间:2013-12-05 18:51:56

标签: c#

我正在尝试将值写入 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);
        }

    }

2 个答案:

答案 0 :(得分:3)

嗯,这个错误对我来说非常清楚。您正在尝试设置DateTime的文本。 DateTime没有Text属性。如果要设置某个标签的文本,请设置标签的文本,而不是DateTime的文本。

答案 1 :(得分:1)

您声明了一个“隐藏”标签的本地变量:

 DateTime CurrentRent;

只是评论出来:

 // Don't include this - it hides your label:
 // DateTime CurrentRent;  

由于这是在本地声明的,因此您尝试设置本地CurrentRent变量的“Text”属性,而不是您的标签。