Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. If Else Loop With Date Format in VB.NET

If Else Loop With Date Format in VB.NET

Scheduled Pinned Locked Moved Visual Basic
helpcsharpdebugging
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tram88
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • T Tram88

      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

      S Offline
      S Offline
      Steve Pullan
      wrote on last edited by
      #2

      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 :-)

      T 1 Reply Last reply
      0
      • S Steve Pullan

        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 :-)

        T Offline
        T Offline
        Tram88
        wrote on last edited by
        #3

        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

        S 2 Replies Last reply
        0
        • T Tram88

          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

          S Offline
          S Offline
          Steve Pullan
          wrote on last edited by
          #4

          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 :-)

          1 Reply Last reply
          0
          • T Tram88

            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

            S Offline
            S Offline
            Steve Pullan
            wrote on last edited by
            #5

            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 Integer

            While eoRead.Peek <> -1 'reads until peek is -1, which is end of file
            eLineOut = eoRead.ReadLine

            If (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 :-)

            T 1 Reply Last reply
            0
            • S Steve Pullan

              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 Integer

              While eoRead.Peek <> -1 'reads until peek is -1, which is end of file
              eLineOut = eoRead.ReadLine

              If (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 :-)

              T Offline
              T Offline
              Tram88
              wrote on last edited by
              #6

              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

              S 1 Reply Last reply
              0
              • T 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

                S Offline
                S Offline
                Steve Pullan
                wrote on last edited by
                #7

                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 and eIntotalCount 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 :-)

                T 1 Reply Last reply
                0
                • S Steve Pullan

                  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 and eIntotalCount 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 :-)

                  T Offline
                  T Offline
                  Tram88
                  wrote on last edited by
                  #8

                  It's working now, Thank you very much Steve. Tram88

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups