Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 构造函数应具有指定的参数_C#_Constructor - Fatal编程技术网

C# 构造函数应具有指定的参数

C# 构造函数应具有指定的参数,c#,constructor,C#,Constructor,你好,我正在做学校作业。我的程序中的一切都很好,但老师希望我在一个构造函数中添加DateTime类型的参数。我有点困惑,因为我认为我已经有了这种类型的参数: using System; using System.Windows.Forms; namespace Assignment4 { class Task { private string time = string.Empty; private string date = string.Em

你好,我正在做学校作业。我的程序中的一切都很好,但老师希望我在一个构造函数中添加DateTime类型的参数。我有点困惑,因为我认为我已经有了这种类型的参数:

using System;
using System.Windows.Forms;

namespace Assignment4
{
    class Task
    {
        private string time = string.Empty;
        private string date = string.Empty;
        private DateTime dateTime = new DateTime();
        private string description = string.Empty;
        private object priorityType;
        private string priority;

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }


        public DateTime DateTime
        {
            set
            {   
                dateTime = value;
                time = dateTime.TimeOfDay.ToString();
                date = dateTime.Date.ToString("d");
            }
        }

        public string Time
        {
            get
            {
                return time;
            }
        }

        public string Date
        {
            get
            {
                return date;
            }
        }


        public object PriorityType
        {
            set
            {
                priorityType = value;
                priority = priorityType.ToString();
            }
        }

        public string Priority
        {
            get
            {
                return priority;
            }
        }
    }
}

dateTime=value不是dateTime类型的参数吗

由于
DateTime
是一个不可变的结构,因此只能从构造函数设置其值。这意味着您需要执行以下操作:

dateTime = new DateTime(2016, 05, 03);
在您的情况下,您可以直接使用它,因为您将它设置在其他地方:

private DateTime dateTime;

(另外,您的属性也需要一个
get
,您现在只需要一个
set

因为
DateTime
是一个不可变的结构,您只能从构造函数设置它的值。这意味着您需要执行以下操作:

dateTime = new DateTime(2016, 05, 03);
在您的情况下,您可以直接使用它,因为您将它设置在其他地方:

private DateTime dateTime;
(另外,您的属性也需要一个
get
,您现在只需要一个
set

该类是一个没有返回类型且具有相同类名的方法。每次创建类的实例(new YourClass)时,都会调用构造函数

您可以将许多具有不同类型参数的构造函数传递给这些方法,即使其中一个没有参数(它是默认构造函数)。
正确的构造函数由创建类时传递的参数标识

public class Person
{
   private string _name;
   private DateTime _dob;
   public Person(string name, DateTime dateOfBirth)
   {
       _name = name;
       _dob = dateOfBirth;
   }
}


..... somewhere in your code .....
Person myself = new Person("Steve", new DateTime(1970,1,1));
该类是一个没有返回类型且与该类同名的方法。每次创建类的实例(new YourClass)时,都会调用构造函数

您可以将许多具有不同类型参数的构造函数传递给这些方法,即使其中一个没有参数(它是默认构造函数)。
正确的构造函数由创建类时传递的参数标识

public class Person
{
   private string _name;
   private DateTime _dob;
   public Person(string name, DateTime dateOfBirth)
   {
       _name = name;
       _dob = dateOfBirth;
   }
}


..... somewhere in your code .....
Person myself = new Person("Steve", new DateTime(1970,1,1));

这是什么课。请出示班级声明。这不是一个构造函数,而是一个property@Steve我想问题在于
=newdatetime()…这是什么类。请出示班级声明。这不是一个构造函数,而是一个property@Steve我想问题在于
=newdatetime()…这是一个很好的猜测,没有看到他的类声明!这可能是整个类声明@DGibbsi现在发布了整个脚本。@帕特里克霍夫曼我同意,然而,这行的措辞可能是
老师希望我在我的一个构造函数中添加DateTime类型的参数
给人的印象是,他可能有自己的自定义类,其中定义了一些需要更改的构造函数-就像我说的,这是一个很好的猜测,没有所有的信息。这是一个很好的猜测,没有看到他的类声明!这可能是整个类声明@DGibbsi现在发布了整个脚本。@帕特里克霍夫曼我同意,然而,这一行的措辞可能是
老师希望我在我的一个构造函数中添加DateTime类型的参数
给人的印象是,他可能有自己的自定义类,其中定义了一些需要更改的构造函数-就像我说的,这是一个很好的猜测,但没有所有的信息。