如何在新构造函数中设置DateTime值?

时间:2018-05-02 11:24:35

标签: c#

我正在我的客户类中从我的构造函数创建一个新客户,我无法让新客户在我的main方法中接受DateTime值。我已尝试多种格式的条目,没有一个有效。如何让新客户接受DateTime输入。

这是我的客户类:


List<Object[]> = em.createNativeQuery(" select first_name, last_name, email from employee").getResultList();

这是我的主要方法:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;



    class Customer
    {

        //Strings

        private string _LastName;
        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }
        private string _FirstName;
        public string FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }
        private string _Address;
        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        private Int32 _ContactNumber;
        public Int32 ContactNumber
        {
            get { return _ContactNumber; }
            set { _ContactNumber = value; }
        }
        private string _EmailAddress;
        public string EmailAddress
        {
            get { return _EmailAddress; }
            set { _EmailAddress = value; }
        }
        private DateTime _Dob;
        public DateTime Dob
        {
            get { return _Dob; }
            set { _Dob = value; }
        }

        //Accounts
        private List<Account> _Accounts = new List<Account>();
        public ReadOnlyCollection<Account> Accounts
        {
            get { return _Accounts.AsReadOnly(); }
        }

        //Adding Accounts
        public void AddAccount(Account account)
        {
            _Accounts.Add(account);
        }

        // Constructor
        public Customer(string LastName, string FirstName, string Address, int ContactNumber, string EmailAddress, DateTime Dob)
        {
            _LastName = LastName;
            _FirstName = FirstName;
            _Address = Address;
            _ContactNumber = ContactNumber;
            _EmailAddress = EmailAddress;
            _Dob = Dob;
        }



    }

为进一步澄清,我指的是我的主要方法,并在新客户中出现错误,特别是“DateTime(02,10,1990)”

2 个答案:

答案 0 :(得分:1)

尝试使用“new DateTime(1900,02,10)”

格式为年,月,日..您还需要使用new这个词来制作新的DateTime对象。

答案 1 :(得分:0)

只需添加新关键字并修复属性顺序:

new DateTime(1990, 2, 10)