If Else Loop With Date Format in VB.NET
-
I know this is very simple to you, but I could not debug the problem. I am writting a program to read everyline of a textfile. If the line has date as my format and an "Error" string, then it must go to the IF/THEN ELSE loop. But in fact, it can go to the IF/THEN ELSE loop even one of the condition is met. If I run my code today which is 8/1/2006, I expect to see is "8/1/2006 4:01:14 Error", but in fact, the result is 2 lines "7/1/2006 4:01:14 Error" and "8/1/2006 4:01:14 Error" Would you please help me out. I would appreciate your big helps very much. Below is my text and code. Tram88 This is the content of text file:----------------------------------------------------------------- 7/1/2006 4:01:14 Error 8/1/2006 4:01:14 Error This is the VB.NET code:---------------------------------------------------------------------------
Dim eoFile As System.IO.File Dim eoRead As System.IO.StreamReader Dim ebStart As Boolean Dim eStart As String = Format(Today, "M" & "/" & "d" & "/" & "yyyy") Dim eEnd As String = "email notification started" eoRead = eoFile.OpenText(strFileEmailNotification) 'opens text file for reading Dim Pass As Integer = 0 Dim eIntotalCount As Integer While eoRead.Peek <> -1 'reads until peek is -1, which is end of file Dim eLineOut = eoRead.ReadLine Dim estringtext As String = "Error" Dim eiLine As Integer If (InStr(1, eLineOut, estringtext, CompareMethod.Text)) And (InStr(1, eLineOut, eStart, CompareMethod.Text)) Then intTotalCount = CountStringInFile(strFileEmailNotification, estringtext) Pass = 1 End If End While
-
I know this is very simple to you, but I could not debug the problem. I am writting a program to read everyline of a textfile. If the line has date as my format and an "Error" string, then it must go to the IF/THEN ELSE loop. But in fact, it can go to the IF/THEN ELSE loop even one of the condition is met. If I run my code today which is 8/1/2006, I expect to see is "8/1/2006 4:01:14 Error", but in fact, the result is 2 lines "7/1/2006 4:01:14 Error" and "8/1/2006 4:01:14 Error" Would you please help me out. I would appreciate your big helps very much. Below is my text and code. Tram88 This is the content of text file:----------------------------------------------------------------- 7/1/2006 4:01:14 Error 8/1/2006 4:01:14 Error This is the VB.NET code:---------------------------------------------------------------------------
Dim eoFile As System.IO.File Dim eoRead As System.IO.StreamReader Dim ebStart As Boolean Dim eStart As String = Format(Today, "M" & "/" & "d" & "/" & "yyyy") Dim eEnd As String = "email notification started" eoRead = eoFile.OpenText(strFileEmailNotification) 'opens text file for reading Dim Pass As Integer = 0 Dim eIntotalCount As Integer While eoRead.Peek <> -1 'reads until peek is -1, which is end of file Dim eLineOut = eoRead.ReadLine Dim estringtext As String = "Error" Dim eiLine As Integer If (InStr(1, eLineOut, estringtext, CompareMethod.Text)) And (InStr(1, eLineOut, eStart, CompareMethod.Text)) Then intTotalCount = CountStringInFile(strFileEmailNotification, estringtext) Pass = 1 End If End While
I would write this a bit differently but I think this line ...
Dim eStart As String = Format(Today, "M" & "/" & "d" & "/" & "yyyy")
should be ...Dim eStart As String = Format(**Now()**, "M" & "/" & "d" & "/" & "yyyy")
...Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-) -
I would write this a bit differently but I think this line ...
Dim eStart As String = Format(Today, "M" & "/" & "d" & "/" & "yyyy")
should be ...Dim eStart As String = Format(**Now()**, "M" & "/" & "d" & "/" & "yyyy")
...Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-) -
Dear Steve, First, I would say thank you very much for your consideration on my problem. However, I did apply your code but it is still the same, that means 2 lines can drop into the loop. I really get stuck on this, please help me out. Thanks, Tram88
In that case there could be something wrong with the data that's being read. Run the code in the debugger and single-step it. Whilst doing this, watch the various variables for correct content. Something is not what you assume it to be. From there we can make a better diagnosis.
...Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-) -
Dear Steve, First, I would say thank you very much for your consideration on my problem. However, I did apply your code but it is still the same, that means 2 lines can drop into the loop. I really get stuck on this, please help me out. Thanks, Tram88
You could also change this code to have the DIM statements outside of the While loop. Note also the change to the If conditions (i.e. testing for a return value > 0 from InStr() rather than relying on an implicit type conversin to Boolean).
Dim eLineOut As String
Dim estringtext As String = "Error"
Dim eiLine As IntegerWhile eoRead.Peek <> -1 'reads until peek is -1, which is end of file
eLineOut = eoRead.ReadLineIf (InStr(1, eLineOut, estringtext, CompareMethod.Text) > 0) And (InStr(1, eLineOut, eStart, CompareMethod.Text) > 0) Then intTotalCount = CountStringInFile(strFileEmailNotification, estringtext) Pass = 1 End If
End While
...Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-) -
You could also change this code to have the DIM statements outside of the While loop. Note also the change to the If conditions (i.e. testing for a return value > 0 from InStr() rather than relying on an implicit type conversin to Boolean).
Dim eLineOut As String
Dim estringtext As String = "Error"
Dim eiLine As IntegerWhile eoRead.Peek <> -1 'reads until peek is -1, which is end of file
eLineOut = eoRead.ReadLineIf (InStr(1, eLineOut, estringtext, CompareMethod.Text) > 0) And (InStr(1, eLineOut, eStart, CompareMethod.Text) > 0) Then intTotalCount = CountStringInFile(strFileEmailNotification, estringtext) Pass = 1 End If
End While
...Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-)Dear Steve, Based on your code, I do have a question for intTotalCount = CountStringInFile(strFileEmailNotification, estringtext) If I run the code, I would like to see intTotalCount=1 . In fact, it is 2. Can you teach me how to program it. Once again, thank you Steve, Tram88
-
Dear Steve, Based on your code, I do have a question for intTotalCount = CountStringInFile(strFileEmailNotification, estringtext) If I run the code, I would like to see intTotalCount=1 . In fact, it is 2. Can you teach me how to program it. Once again, thank you Steve, Tram88
Tram88 wrote:
Based on your code, I do have a question for intTotalCount = CountStringInFile(strFileEmailNotification, estringtext) If I run the code, I would like to see intTotalCount=1 . In fact, it is 2. Can you teach me how to program it.
The
CountStringInFile()
function is not a standard VB function call. I presume you have obtained this code from somewhere else? If it does what its name implies, then it will scan the entire file for any occurrences of the search string - in this particular case with the data you have already supplied in your first post, there are two lines which contain "Error" therefore the return value will be 2. What I think you need to do is to change this line:intTotalCount = CountStringInFile(strFileEmailNotification, estringtext)
to
eIntotalCount = eIntotalCount + 1
since
intTotalCount
does not seem to be declared anywhere andeIntotalCount
is declared but not used. [Was this intentional?] When your code exits,eIntotalCount
will now contain the count of the number of lines in your file that have both the error string AND the date string for which you are searching....Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-) -
Tram88 wrote:
Based on your code, I do have a question for intTotalCount = CountStringInFile(strFileEmailNotification, estringtext) If I run the code, I would like to see intTotalCount=1 . In fact, it is 2. Can you teach me how to program it.
The
CountStringInFile()
function is not a standard VB function call. I presume you have obtained this code from somewhere else? If it does what its name implies, then it will scan the entire file for any occurrences of the search string - in this particular case with the data you have already supplied in your first post, there are two lines which contain "Error" therefore the return value will be 2. What I think you need to do is to change this line:intTotalCount = CountStringInFile(strFileEmailNotification, estringtext)
to
eIntotalCount = eIntotalCount + 1
since
intTotalCount
does not seem to be declared anywhere andeIntotalCount
is declared but not used. [Was this intentional?] When your code exits,eIntotalCount
will now contain the count of the number of lines in your file that have both the error string AND the date string for which you are searching....Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-)