Click here to Skip to main content
15,909,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not sure how to go about declaring a datetime type variable in my constructor:

C#
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;
   
    }


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

C#
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.
Posted
Comments
phil.o 27-Feb-14 5:17am    
Not clear: what do you have problems with? The declaration of a DateTime variable? Its use in a class constructor?
miss786 27-Feb-14 5:20am    
hi, thank you for your reply. That is correct, how do i go about declaring the DateTime variable in the 'data' class? Many thanks

Probably the easiest is just:
C#
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...
 
Share this answer
 
Comments
miss786 27-Feb-14 5:35am    
Thank you so much for your suggestions, I manage to declare the variable using your suggestion. I have come across a little error in converting the datetime to string and I am experiencing - Cannot implicitly convert type 'string' to 'System.DateTime' -- error, on the following code:

List dataList = new List();
string cat="";
float val=0;
string typ = "";
DateTime dat = DateTime.Now;

dat.ToString("ddmmYYYY", System.Globalization.CultureInfo.InvariantCulture);

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

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

typ = dr[2].ToString();

dat = Convert.ToString(dr[3]);

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

Thank you for your time and help.
OriginalGriff 27-Feb-14 5:58am    
Well, no...
DateTime dat = DateTime.Now;
...
dat = Convert.ToString(dr[3]);
So why are you converting it to a string?
Why not:
dat = (DateTime) dr[3];
Assuming the column is a DateTime?
Not sure to understand your problem clearly. It could be as simple as:
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900