Click here to Skip to main content
15,886,796 members
Home / Discussions / C#
   

C#

 
AnswerRe: Objects created in a loop - definition name Pin
Jean A Brandelero8-Aug-13 10:43
Jean A Brandelero8-Aug-13 10:43 
QuestionWah Wah effect for guitar Pin
paulera6-Aug-13 1:52
paulera6-Aug-13 1:52 
AnswerRe: Wah Wah effect for guitar Pin
Ravi Bhavnani6-Aug-13 6:02
professionalRavi Bhavnani6-Aug-13 6:02 
GeneralRe: Wah Wah effect for guitar Pin
paulera6-Aug-13 8:21
paulera6-Aug-13 8:21 
AnswerRe: Wah Wah effect for guitar Pin
MarkRHolbrook11-Aug-13 4:04
MarkRHolbrook11-Aug-13 4:04 
GeneralAdding digital signature in Crystal report pdf Pin
gandhipurvi6-Aug-13 0:25
gandhipurvi6-Aug-13 0:25 
GeneralRe: Adding digital signature in Crystal report pdf Pin
Ennis Ray Lynch, Jr.6-Aug-13 2:55
Ennis Ray Lynch, Jr.6-Aug-13 2:55 
QuestionDatetime parsing Pin
ExcellentOrg5-Aug-13 21:58
ExcellentOrg5-Aug-13 21:58 
This is one intriguing error that I do not even know whether to call it a bug or not ... It is fairly likely that this thing is already solved somehow by locale of which I am yet to get the precise clue.

OK! all of the contents of this post (rant) revolves around DateTime, something that we, fortunately agree on values but just cannot have uniformity on how we'd like to store, read on screen and pass around.

I am writing one .net application in C# that parses dates and times coming in from a ASCII bytestream. This problem only occurs when date is sent as ASCII string. As a part of early design decision,I had decided that I decided to keep will only keep one dedicated function to parse strings into dates.

At first, this function started with "dd/MM/yyyy HH:mm:ss". In a different record, I discovered "yyyyMMdd". In yet another place, I discovered only date (no time) "dd-MMM-yyyy". Given that I am in India, this is the defacto date format used, so no problems.

All of these were very efficiently handled by DateTime.TryParse no problems, but then I started getting dates being sent as "MM/dd/yyyy HH:mm:ss" which is like a giant monkey spanner at TryParse regardless of the locale setting. If locale is set to American, 07/01 gets read as July 1. If I set locale to British, 07/01 gets read 7 January. At least some of the byte stream format will be read incorrectly if I use same locale setting!!!

At least for first 12 days of all month, this would read date incorrectly!!! Cannot use TryParse. Cannot tell people sending me byte-stream to fix up the mess. I want data, so its my headache on how to read it... Fortunately, I do tag attributes on field on what format date is it going to be in.

All in all, between 50 different byte-streams, I get string dates in formats listed below. List is sorted by frequency of occurrence. Rest of them come either as # of seconds from some epoch or pair of integers.

"MM/dd/yyyy HH:mm:ss",
"dd/MM/yyyy HH:mm:ss",
"yyyy-MM-dd HH:mm:ss.fff",
"yyyy-MM-dd HH:mm:ss.ff",
"yyyy-MM-dd HH:mm:ss.f",
"ddMMMyyyy",
"yyyyMMdd",
"dd-MMM-yyyy"

fff are miliseconds and honestly only one should be needed, but I do get 1, 2 or 3 digits after the decimal!!!

In my function, I've plonked the all format strings into a static String array and then loop over it like this ... try catch inside the loop allows loop to go to next format if current one fails.

for (int i = 0; i < DateFormatStrings.Length; i++)
{
try
{
dt = DateTime.ParseExact(formatDate, DateFormatStrings[i], null);

fInfo.SetValue(this, dt);

if (formatFailed > 0)
Logger.LogLine(String.Format("Field {0} in class {1}, was finally able to parse date [{3}] in pre-defined format {4}",
fInfo.Name, myType, formatDate, DateFormatStrings[i]), LoggingLevel.HighLevelDebugging);

break;
}
catch (FormatException)
{
formatFailed++;
Logger.LogLine(String.Format("Field {0} in class {1}, was unable to parse date [{2}] in pre-defined format {3}",
fInfo.Name, myType.Name, formatDate, DateFormatStrings[i]), LoggingLevel.HighLevelDebugging);
fInfo.SetValue(this, null);
}
}

Now the most funny thing at which I can neither laugh nor cry is.
that this loop ParseExact fails with FormatException whenever date comes in as 2013-07-31 09:34:44.74

The format string to parse it correctly is present in array at index 3 and it does get looped over too throwing exception.

In another function, with following code, this works without format exception...

DateTime dt = DateTime.ParseExact(
"2013-07-31 09:34:44.74", "yyyy-MM-dd HH:mm:ss.ff", null);

In other words, ParseExact fails with exception, when inside try-catch block and works fine when not enclosed.

[REPOSTED!!!]

modified 6-Aug-13 5:30am.

AnswerRe: Datetime parsing Pin
Richard MacCutchan5-Aug-13 22:58
mveRichard MacCutchan5-Aug-13 22:58 
GeneralRe: Datetime parsing Pin
ExcellentOrg5-Aug-13 23:30
ExcellentOrg5-Aug-13 23:30 
AnswerRe: Datetime parsing Pin
Richard MacCutchan6-Aug-13 0:04
mveRichard MacCutchan6-Aug-13 0:04 
GeneralRe: Datetime parsing Pin
ExcellentOrg6-Aug-13 9:19
ExcellentOrg6-Aug-13 9:19 
AnswerRe: Datetime parsing Pin
Richard MacCutchan6-Aug-13 0:09
mveRichard MacCutchan6-Aug-13 0:09 
GeneralRe: Datetime parsing Pin
ExcellentOrg6-Aug-13 9:10
ExcellentOrg6-Aug-13 9:10 
AnswerRe: Datetime parsing Pin
Mycroft Holmes6-Aug-13 12:56
professionalMycroft Holmes6-Aug-13 12:56 
GeneralRe: Datetime parsing Pin
ExcellentOrg6-Aug-13 18:52
ExcellentOrg6-Aug-13 18:52 
AnswerRe: Datetime parsing Pin
ExcellentOrg7-Aug-13 7:57
ExcellentOrg7-Aug-13 7:57 
QuestionHow to convert string to form object in c# window application Pin
susanna.floora5-Aug-13 21:37
susanna.floora5-Aug-13 21:37 
AnswerRe: How to convert string to form object in c# window application Pin
ExcellentOrg5-Aug-13 22:34
ExcellentOrg5-Aug-13 22:34 
AnswerRe: How to convert string to form object in c# window application Pin
Rizny Mubarak5-Aug-13 23:43
Rizny Mubarak5-Aug-13 23:43 
QuestionRe: How to convert string to form object in c# window application Pin
Eddy Vluggen6-Aug-13 7:47
professionalEddy Vluggen6-Aug-13 7:47 
AnswerRe: How to convert string to form object in c# window application Pin
susanna.floora7-Aug-13 19:07
susanna.floora7-Aug-13 19:07 
AnswerRe: How to convert string to form object in c# window application Pin
ExcellentOrg6-Aug-13 9:22
ExcellentOrg6-Aug-13 9:22 
AnswerRe: How to convert string to form object in c# window application Pin
Jean A Brandelero8-Aug-13 10:45
Jean A Brandelero8-Aug-13 10:45 
GeneralRe: How to convert string to form object in c# window application Pin
susanna.floora9-Aug-13 2:02
susanna.floora9-Aug-13 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.