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

时间:2014-04-14 06:50:58

标签: c#

如何初始化具有开始日期和结束日期作为参数的t1对象?错误行是t1.store(" task1"," gui design",2014 01 01,2014 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);
    }
}

}

4 个答案:

答案 0 :(得分:1)

您可以通过这些方法创建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.Date或end.Date

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您的参数不正确。 你必须使用这个数字: 新日期(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);
    }
}

答案 3 :(得分:0)

由于您使用的是custom Date implementation,因此调用该方法的正确方法如下:

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