DateTime::Now

ramCC

Member
Joined
Jun 7, 2004
Messages
9
Anybody with a clue on why this code (man c++):

String *myTime = DateTime::Now.ToString(L"dd/MM/yyyy HH:mm:ss", DateTimeFormatInfo::InvariantInfo);

sometimes facilitate myTime with the correct time , and sometimes just stayed <undefined> (NULL) .

Thanks.
 
Ive noticed that .NET 2003 seems a lot "smarter" when youre debugging. If you define a simple int and set it to 0 and run it (never using it again in a function), the compiler will often show "undefined value" even though you clearly set it to 0. The same goes for objects that are defined but never "used". The compiler, I think, is trimming out that code since it detects it never being used.

In the cases where it says "undefined value" are you using the variable farther down, or just testing the initialization? Try creating a class level field and setting that if you just need to test.

If thats not it, let me know and Ill see if I can narrow it down.

-Nerseus
 
Hey Nerseus, thanks for your reply !

I am using the object further down, so I guess its not a compiler issue.
the relevant function is:

void txMgr::AddLogEntry(String *i_logEntry, Char i_warnning[], Char i_error[])
{
String *line = DateTime::Now.ToString(L"dd/MM/yyyy HH:mm:ss", DateTimeFormatInfo::InvariantInfo);
line = line->Concat(i_logEntry);

if(line->Length > 255)
line = line->Substring(0,255);
m_logFile->WriteLine(line);
m_logFile->Flush();
}


The strange thing is that on my log file (m_logFile) on some cases I do see the date prefix and on other cases I dont (only the i_logEntry).

On debugging I see that nothing comes back from DateTime::Now.ToString() even though I do see the Now object (when doing "DateTime now = DateTime::Now") . Maybe there is a problem with the ToString ? I have no idea ...
 
Offhand, I cant think of any reason why the ToString would ever cause a problem. Have you tried using Debug.Assert right after the line that instantiates *line, to see if its empty string or null?

-ner
 
Thanks Nerseus !

I found the problem with this code, and it had nothing to do with DateTime object or the ToString function.

The problem was the use of Concat ...
I used: line = line->Concat(i_logEntry);
instead of: line = String::Concat(line, i_logEntry);

My common sense told me it will yield the same result, but I guess my common sense has no place in this world ...

Thanks a lot anyways
Ram.
 
Back
Top