声明datetime构造函数? [英] Declaring datetime constructor?

查看:108
本文介绍了声明datetime构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在构造函数中声明datetime类型变量:



I am not sure how to go about declaring a datetime type variable in my constructor:

public string ColumnName = "";
    public float Value = 0;
    public string Type = "";
    public DateTime Date = 
   

 
    public Data(string columnName, float value, string type)
    {
        ColumnName = columnName;
        Value = value;
        Type = type;
   
    }





我打算用以下方法链接'date'变量:





I plan to link the 'date' variable in the following method:

List<Data> dataList = new List<Data>();
        string cat="";
        float val=0;
        string typ = "";

        foreach (DataRow dr in dt.Rows)
        {
            try
            {
                cat = dr[0].ToString();

                val = Convert.ToInt32(dr[1]);

                typ = dr[2].ToString();

            }
            catch
            {
            }
            dataList.Add(new Data(cat, val, typ));
        }
        return dataList;
    }





非常感谢任何帮助。非常感谢。



Any help would be very much appreciated. Many thanks.

推荐答案

可能最简单的只是:

Probably the easiest is just:
public DateTime Date = DateTime.Now;

这给了它创建时间的默认值。



但请不要公开字段:使用属性代替!这将来会更安全......

Which gives it a default value of the creation time.

But please, don't make fields public: use properties instead! It's a lot safer in the future...


不确定清楚地理解你的问题。它可以很简单:

Not sure to understand your problem clearly. It could be as simple as:
public string ColumnName = "";
public float Value = 0;
public string Type = "";
public DateTime Date = DateTime.Empty;
// Other possibilities
// public DateTime Date = DateTime.Now;
// public DateTime Date = DateTime.Today;
// public DateTime Date = new DateTime(1973, 9, 29, 4, 30, 0);

public Data(string columnName, float value, string type, DateTime date)
{
   ColumnName = columnName;
   Value = value;
   Type = type;
   Date = date;
}





你应该看看:

日期时间结构 [ ^ ]



希望这会有所帮助。祝你好运。



You should have a look at:
DateTime Structure[^]

Hope this helps. Good luck.


这篇关于声明datetime构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆