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

passing date argument to store date in object variable in c#
2021-03-07
  •  译文(汉语)
  •  原文(英语)

如何初始化以开始和结束日期为参数的t1对象?错误行是t1.store("task1","gui design",2014 01 01,2014 04 04,"completed").如何将日期参数传递给存储方法...谁能帮助我找出答案?

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);
    }
}

}

速聊1:
这完全取决于它是什么Date以及提供的构造函数.这里的任何人都不可能回答这个问题.最有可能涉及诸如之类的代码new Date(...),然后您必须弄清楚如何将拥有的值放入该构造函数的参数中.
速聊2:
我再改一下.Date不是内置的.NET类型.这种类型要么来自您自己的代码,要么来自您正在使用的库,但是除非我们知道它来自哪个库,或者可以看到其构造函数的代码,否则无法回答您的问题.
速聊3:
是的,我正在从库github.com/claycephus/csharp-date使用"日期"
解决过程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:
他没有使用DateTime,Date不是.NET类型,这里没有任何人可以回答他的问题.
速聊2:
那么问题是什么呢?当然,您可以在其中看到Date的构造函数吗?如果键入new Date(,会发生什么情况?您会从Visual Studio获得帮助吗?
速聊3:
.Karlsen`public void store(string tid,string tname,DateTime start,DateTime end,`参数的定义是,DateTime因此他将需要一个DateTime对象作为参数
速聊4:
不,参数的定义是Date,不是DateTime.
速聊5:
.Karlsen是的,您是对的,我编辑了答案
解决过程2
t1.store("task1","gui design",Convert.ToDateTime(01/01/2014),Convert.ToDateTime(04/04/2014),"completed");
解决过程3

您的参数不正确.您必须使用以下数字:new Date(2014 01 01)并通过tow方法;

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);
    }
}
解决过程4

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

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

how can i initialize t1 object which has start and end dates as parameters? the error line is t1.store("task1","gui design",2014 01 01,2014 04 04,"completed") . how can i pass date argument to store method... can anyone help me to find out?

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);
    }
}

}

Talk1:
That depends entirely on what Date is and what constructors it provides. There's no way anyone here can answer this question. Most likely it involves code like new Date(...), and then you have to figure out how to put the values you have into the parameters to that constructor.
Talk2:
Let me rephrase that. Date is not a built-in .NET type. This type either comes from your own code, or from a library you're using, but unless we either know which library it came from, or can see the code for its constructor(s), there's no way to answer your question.
Talk3:
yes i am using 'Date' from library github.com/claycephus/csharp-date
Solutions1

You can create DateTime via these methods

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

and change the method signature to this

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

and if you want to be sure that you only use the Date part of DateTime object you can use start.Date or end.Date

Talk1:
He's not using DateTime, he's using Date, which is not a .NET type, there's no way anyone here can answer his question.
Talk2:
So what is the question? Surely you can see the constructor for Date there? What happens if you type new Date(, do you get help from Visual Studio telling you what goes where?
Talk3:
.Karlsen ` public void store(string tid, string tname, DateTime start, DateTime end,` the definition of parameters is DateTime so he would need a DateTime object as param
Talk4:
No, the definition of the parameters is Date, not DateTime.
Talk5:
.Karlsen Yes, you are right, I edited my answer
Solutions2
t1.store("task1","gui design",Convert.ToDateTime(01/01/2014),Convert.ToDateTime(04/04/2014),"completed");
Solutions3

your parameter is incorrect. you must use this figure: new Date(2014 01 01) and pass tow method;

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);
    }
}
Solutions4

Since you're using a custom Date implementation, the correct way to call that method is like this:

t1.store("task1","gui design", new Date(2014, 01, 01), new Date(2014, 04, 04) ,"completed");
转载于:https://stackoverflow.com/questions/23053695/passing-date-argument-to-store-date-in-object-variable-in-c-sharp

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入