传递日期参数以将日期存储在c#中的对象变量中

本文关键字:日期 对象 变量 存储 参数 | 更新日期: 2023-09-27 17:58:07

如何初始化以开始日期和结束日期为参数的t1对象?错误行是t1.store("task1","gui设计",2014 01 012014 04 04,"已完成")。如何将日期参数传递给存储方法。。。有人能帮我查一下吗?

class Program
{
    static void Main(string[] args)
    {
        Task t1 = new Task();
        Task t2 = new Task();
        t1.store("task1","gui design",2014 01 01,2014 04 04,"completed");
    }
}
class Task 
{
    string _Tid;
    string _tn;
    DateTime _sdate;
    DateTime _edate;
    string _status;
    public void store(string tid,string tname, Date start,Date end,string sts) 
    {
        this._Tid = tid;
        this._tn = tname;
        this._sdate = start;
        this._edate = end;
        this._status = sts;
    }
    public void print() 
    {
        Console.WriteLine("'n {0}'t{1}'t{2}'t{3}'t{4}",this._Tid,this._tn,this._sdate,this._edate,this._status);
    }
}

}

传递日期参数以将日期存储在c#中的对象变量中

您可以通过这些方法创建DateTime

            var dt1 = new DateTime(2014,10,25);
            var dt2 = DateTime.Parse("2014/10/25");

并将方法签名更改为此

public void store(string tid,string tname, DateTime start,DateTime end,string sts)

如果您想确保只使用DateTime对象的Date部分,则可以使用start。日期或结束。日期

t1.store("task1","gui design",Convert.ToDateTime(01/01/2014),Convert.ToDateTime(04/04/2014),"completed");

您的参数不正确。您必须使用以下数字:新日期(2014年01月01日)和通过拖曳法;

class Program
{
    static void Main(string[] args)
    {
         Task t1 = new Task();
         Task t2 = new Task();
         t1.store("task1","gui design",new DateTime(2014, 01, 01),new DateTime(2014 ,04 ,04),"completed");
    }
}
class Task
{
    string _Tid;
    string _tn;
    DateTime _sdate;
    DateTime _edate;
    string _status;
    public void store(string tid, string tname, DateTime start, DateTime end, string sts)
    {
        this._Tid = tid;
        this._tn = tname;
        this._sdate = start;
        this._edate = end;
        this._status = sts;
    }
    public void print()
    {
        Console.WriteLine("'n {0}'t{1}'t{2}'t{3}'t{4}", this._Tid, this._tn, this._sdate, this._edate, this._status);
    }
}

由于您使用的是自定义Date实现,因此调用该方法的正确方法如下:

t1.store("task1","gui design", new Date(2014, 01, 01), new Date(2014, 04, 04) ,"completed");