Click here to Skip to main content
15,915,163 members
Home / Discussions / C#
   

C#

 
Questionreading text file and calculating the working hours for each employee Pin
emmy_23200326-May-09 22:14
emmy_23200326-May-09 22:14 
QuestionRe: reading text file and calculating the working hours for each employee Pin
Rajesh R Subramanian26-May-09 22:22
professionalRajesh R Subramanian26-May-09 22:22 
AnswerRe: reading text file and calculating the working hours for each employee Pin
emmy_23200326-May-09 22:25
emmy_23200326-May-09 22:25 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff26-May-09 22:36
mveOriginalGriff26-May-09 22:36 
GeneralRe: reading text file and calculating the working hours for each employee Pin
emmy_23200326-May-09 22:47
emmy_23200326-May-09 22:47 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff26-May-09 23:02
mveOriginalGriff26-May-09 23:02 
GeneralRe: reading text file and calculating the working hours for each employee Pin
emmy_23200327-May-09 0:16
emmy_23200327-May-09 0:16 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff27-May-09 0:48
mveOriginalGriff27-May-09 0:48 
First, a couple of things to make both our lives easier.
1) Before you paste a code fragment, there is a check box above the "Post message" button, labeled "Encode HTML tags when pasting". If this has a tick in it, the greater than and less than signs are replaced with & g t ; and & l t ; - untick it before you paste a code fragment.
2) Just above the line of smileys is "code block" - this encases your code fragment in <pre> and </pre> so that formatting is preserved.
Without:
DateTime date1;
DateTime date2;
string[] allLines = System.IO.File.ReadAllLines(@"C:\XXTemp\new.txt.txt");
foreach (string eachRow in allLines)
{
string[] rowaArray = eachRow.Split(',');
string enrollID = rowaArray[1];
date1 = DateTime.Now;
date2 = System.Convert.ToDateTime(rowaArray[3] + " " + rowaArray[5]);
TimeSpan ts = date1.Subtract(date2);
MessageBox.Show(ts.Days + " Days " + ts.Hours + " Hours " + ts.Seconds + " Seconds ");
}
With:
DateTime date1;
DateTime date2;
string[] allLines = System.IO.File.ReadAllLines(@"C:\XXTemp\new.txt.txt");
foreach (string eachRow in allLines)
    {
    string[] rowaArray = eachRow.Split(',');
    string enrollID = rowaArray[1];
    date1 = DateTime.Now;
    date2 = System.Convert.ToDateTime(rowaArray[3] + " " + rowaArray[5]);
    TimeSpan ts = date1.Subtract(date2);
    MessageBox.Show(ts.Days + " Days " + ts.Hours + " Hours " + ts.Seconds + " Seconds ");
    }


As you can see, I have changed yours a little - but not too much.

Try to use foreach, rather than a for loop - it is more readable and obvious.

You don't need all those .ToString() convertions: they are already strings!

What you call "highlighting" I assume means "making it go green"? If so, this is called commenting out - it turns the line from something the compiler will read into something only people will read.

Without the 'rowaArray[3] + " " + rowaArray[5]' bit, you are always ignoring the time. I don't know why the index was outside the bounds of the array, but it might be worth adding some error checking to ensure the data in your file is in the format you are expecting, or a duff line could cause that error.

You don't need the "new TimeSpan()" as you are throwing it away on the next line!

I suspect the answers are "wrong right" because your subtract was the wrong way round!

No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.

This message is made of fully recyclable Zeros and Ones

GeneralRe: reading text file and calculating the working hours for each employee Pin
emmy_23200327-May-09 1:22
emmy_23200327-May-09 1:22 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff27-May-09 1:28
mveOriginalGriff27-May-09 1:28 
GeneralRe: reading text file and calculating the working hours for each employee Pin
emmy_23200327-May-09 1:36
emmy_23200327-May-09 1:36 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff27-May-09 1:54
mveOriginalGriff27-May-09 1:54 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff27-May-09 1:55
mveOriginalGriff27-May-09 1:55 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff27-May-09 4:29
mveOriginalGriff27-May-09 4:29 
GeneralRe: reading text file and calculating the working hours for each employee Pin
emmy_23200327-May-09 20:58
emmy_23200327-May-09 20:58 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff27-May-09 22:08
mveOriginalGriff27-May-09 22:08 
GeneralRe: reading text file and calculating the working hours for each employee Pin
emmy_23200328-May-09 1:11
emmy_23200328-May-09 1:11 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff28-May-09 1:24
mveOriginalGriff28-May-09 1:24 
GeneralRe: reading text file and calculating the working hours for each employee Pin
emmy_23200328-May-09 1:28
emmy_23200328-May-09 1:28 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff28-May-09 1:59
mveOriginalGriff28-May-09 1:59 
GeneralRe: reading text file and calculating the working hours for each employee Pin
emmy_23200329-May-09 23:02
emmy_23200329-May-09 23:02 
GeneralRe: reading text file and calculating the working hours for each employee Pin
Henry Minute27-May-09 0:39
Henry Minute27-May-09 0:39 
GeneralRe: reading text file and calculating the working hours for each employee Pin
OriginalGriff27-May-09 0:45
mveOriginalGriff27-May-09 0:45 
QuestionViewer Pin
ellllllllie26-May-09 22:07
ellllllllie26-May-09 22:07 
QuestionA Combobox problem Pin
leung wilson26-May-09 21:51
leung wilson26-May-09 21:51 

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.