开发者

DateTime in winForms

I have the following code within the class.

basically what i need is it to tell me the current DateTim开发者_运维知识库e.

where i have a problem is when compiling, as under the dateTime code there is syntax error saying: "The type 'dateAndTime' already contains a definition for 'dateTime'"

 class dateAndTime
        {
            public dateAndTime dateTime { get; private set; }

            DateTime dateTime = new DateTime();

            DateTime dateTime;

        }
    }

can anyone help with this problem please?

much appreciated!


These three lines are declaring variables (or properties):

public dateAndTime dateTime { get; private set; }
DateTime dateTime = new DateTime();
DateTime dateTime;

No two elements in a class definition can have the same name.

What are you trying to accomplish that can't be done with DateTime.Now?


Just add property to your class( actually you need DateTime.Now):

public DateTime CurrentDateTime
{
  get
    {
      return DateTime.Now;
    }
}


basically what i need is it to tell me the current DateTime.

You don't need your class at all. Just use DateTime.Now.


You can't redeclare a variable with the same name as the property name:

class MyDateAndTime
{

   public DateTime dateTime { get; private set; }

   public MyDateAndTime()
   {
                dateTime = new DateTime();

   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜