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();
}
}
精彩评论