If statement not catching on conditions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmdolcet69

    If statement not catching on conditions

    I have the below if statement, that should catch if any of the
    conditions are met.....however for some reasons if my
    boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
    muxclass.COM1Ac tive =1 it will not exit the program??? Why is that can
    anyone help me

    Urgent!!!!!!



    If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
    Not muxClass.COM1Ac tive = 1 Then
    Exit Sub
    End If
  • Lloyd Sheen

    #2
    Re: If statement not catching on conditions


    "cmdolcet69 " <colin_dolcetti @hotmail.comwro te in message
    news:0f75e370-5f51-4928-8d6a-ef2a3be548d0@m3 2g2000hsf.googl egroups.com...
    >I have the below if statement, that should catch if any of the
    conditions are met.....however for some reasons if my
    boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
    muxclass.COM1Ac tive =1 it will not exit the program??? Why is that can
    anyone help me
    >
    Urgent!!!!!!
    >
    >
    >
    If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
    Not muxClass.COM1Ac tive = 1 Then
    Exit Sub
    End If
    Any = Or
    All = And

    your statement wants all the conditions to be met before the exit will
    occur.

    LS

    Comment

    • =?Utf-8?B?QmV0aA==?=

      #3
      RE: If statement not catching on conditions

      Did you want an OR connector instead of AND?
      You said: should catch if ANY of the conditions are met

      Would
      If (not boolDSIFlushGap Reading) OR (muxClass.DSIVa lues.Count < 2) OR
      (muxClass.COM1A ctive <1) Then

      do it?

      "cmdolcet69 " wrote:
      I have the below if statement, that should catch if any of the
      conditions are met.....however for some reasons if my
      boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
      muxclass.COM1Ac tive =1 it will not exit the program??? Why is that can
      anyone help me
      >
      Urgent!!!!!!
      >
      >
      >
      If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
      Not muxClass.COM1Ac tive = 1 Then
      Exit Sub
      End If
      >

      Comment

      • Jack Jackson

        #4
        Re: If statement not catching on conditions

        On Wed, 29 Oct 2008 10:20:57 -0700 (PDT), cmdolcet69
        <colin_dolcetti @hotmail.comwro te:
        >I have the below if statement, that should catch if any of the
        >conditions are met.....however for some reasons if my
        >boolDSIFlushGa pReading = true and MuxClass.DSIVal ues.count =1 and my
        >muxclass.COM1A ctive =1 it will not exit the program??? Why is that can
        >anyone help me
        >
        >Urgent!!!!!!
        >
        >
        >
        >If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
        >Not muxClass.COM1Ac tive = 1 Then
        Exit Sub
        End If
        Your statement says to execut the Exit Sub only if all three
        conditions are met. In your example the first one is not met and the
        other two are.

        Apparently you want Or instead of And. An Or test is true if either
        item is true, and And test is true of both items are true.

        Also, you should use AndAlso and OrElse instead of And and Or for
        tests like this.

        Comment

        • cmdolcet69

          #5
          Re: If statement not catching on conditions

          On Oct 29, 1:39 pm, Jack Jackson <jjack...@cinno vations.netwrot e:
          On Wed, 29 Oct 2008 10:20:57 -0700 (PDT), cmdolcet69
          >
          <colin_dolce... @hotmail.comwro te:
          I have the below if statement, that should catch if any of the
          conditions are met.....however for some reasons if my
          boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
          muxclass.COM1Ac tive =1 it will not exit the program??? Why is that can
          anyone help me
          >
          Urgent!!!!!!
          >
          If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
          Not muxClass.COM1Ac tive = 1 Then
                         Exit Sub
                     End If
          >
          Your statement says to execut the Exit Sub only if all three
          conditions are met.  In your example the first one is not met and the
          other two are.
          >
          Apparently you want Or instead of And.  An Or test is true if either
          item is true, and And test is true of both items are true.
          >
          Also, you should use AndAlso and OrElse instead of And and Or for
          tests like this.

          Ok a step further lets say:
          boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
          muxclass.COM1Ac tive =2 it will not exit the program???

          How can I fix that.......


          Comment

          • cmdolcet69

            #6
            Re: If statement not catching on conditions

            On Oct 29, 1:27 pm, "Lloyd Sheen" <sql...@hotmail .comwrote:
            "cmdolcet69 " <colin_dolce... @hotmail.comwro te in message
            >
            news:0f75e370-5f51-4928-8d6a-ef2a3be548d0@m3 2g2000hsf.googl egroups.com...
            >
            I have the below if statement, that should catch if any of the
            conditions are met.....however for some reasons if my
            boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
            muxclass.COM1Ac tive =1 it will not exit the program??? Why is that can
            anyone help me
            >
            Urgent!!!!!!
            >
            If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
            Not muxClass.COM1Ac tive = 1 Then
                           Exit Sub
                       End If
            >
            Any = Or
            All = And
            >
            your statement wants all the conditions to be met before the exit will
            occur.
            >
            LS
            No what happends is that DSIValues.Count can =1 but then the
            muxclass.COM1Ac tive = 0
            The only wat it doesn;t exist is that DSIValues.Count can =2 but then
            the muxclass.COM1Ac tive = 1 or

            DSIValues.Count can =1 but then the muxclass.COM2Ac tive = 2

            Comment

            • Armin Zingler

              #7
              Re: If statement not catching on conditions

              "cmdolcet69 " <colin_dolcetti @hotmail.comsch rieb
              I have the below if statement, that should catch if any of the
              conditions are met.....however for some reasons if my
              boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
              muxclass.COM1Ac tive =1 it will not exit the program??? Why is that
              can anyone help me
              >
              Urgent!!!!!!
              >
              >
              >
              If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2
              And Not muxClass.COM1Ac tive = 1 Then
              Exit Sub
              End If

              First I thought it could be a matter of operator precedence
              (http://msdn.microsoft.com/en-us/library/fw84t893.aspx), but it's correct.

              You expect the program to exit. Have you tested if the "Exit sub" statement
              is reached (Debug.Print)? Or maybe it is reached but it doesn't cause the
              expected behavior? Exit Sub exits the sub, not the program.


              Armin

              Comment

              • cmdolcet69

                #8
                Re: If statement not catching on conditions

                On Oct 29, 3:51 pm, "Armin Zingler" <az.nos...@free net.dewrote:
                "cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
                >
                I have the below if statement, that should catch if any of the
                conditions are met.....however for some reasons if my
                boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
                muxclass.COM1Ac tive =1 it will not exit the program??? Why is that
                can anyone help me
                >
                Urgent!!!!!!
                >
                If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2
                And Not muxClass.COM1Ac tive = 1 Then
                               Exit Sub
                           End If
                >
                First I thought it could be a matter of operator precedence
                (http://msdn.microsoft.com/en-us/library/fw84t893.aspx), but it's correct..
                >
                You expect the program to exit. Have you tested if the "Exit sub" statement
                is reached (Debug.Print)? Or maybe it is reached but it doesn't cause the
                expected behavior? Exit Sub exits the sub, not the program.
                >
                Armin
                Armin it never exit the Sub and that all I want it to do is exit the
                sub

                Comment

                • Armin Zingler

                  #9
                  Re: If statement not catching on conditions

                  "cmdolcet69 " <colin_dolcetti @hotmail.comsch rieb
                  Armin it never exit the Sub and that all I want it to do is exit the
                  sub

                  It's possible that debugging has an influence on the result, in particular
                  as you are handling COM ports here (I guess) where the state can change
                  while debugging. For example, if you set a breakpoint and examine the
                  values, they can be different from those if you had not interrupted the
                  execution. Therefore, try this:

                  debug.write(boo lDSIFlushGapRea ding & " ")
                  debug.write(mux Class.DSIValues .Count & " ")
                  debug.writeline (muxClass.COM1A ctive)

                  If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 _
                  And Not muxClass.COM1Ac tive = 1 Then
                  debug.writeline ("exit sub")
                  Exit Sub
                  End If


                  I can not debug it here, so maybe exit sub is reached but the function is
                  called again and you think it is never left? I don't know. Is "exit sub"
                  ever shown in the debug/output window? Do you ever get the value combination
                  that you think should make the sub exit?



                  Armin

                  Comment

                  • Jack Jackson

                    #10
                    Re: If statement not catching on conditions

                    On Wed, 29 Oct 2008 11:38:16 -0700 (PDT), cmdolcet69
                    <colin_dolcetti @hotmail.comwro te:
                    >On Oct 29, 1:39 pm, Jack Jackson <jjack...@cinno vations.netwrot e:
                    >On Wed, 29 Oct 2008 10:20:57 -0700 (PDT), cmdolcet69
                    >>
                    ><colin_dolce.. .@hotmail.comwr ote:
                    >I have the below if statement, that should catch if any of the
                    >conditions are met.....however for some reasons if my
                    >boolDSIFlushGa pReading = true and MuxClass.DSIVal ues.count =1 and my
                    >muxclass.COM1A ctive =1 it will not exit the program??? Why is that can
                    >anyone help me
                    >>
                    >Urgent!!!!!!
                    >>
                    >If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
                    >Not muxClass.COM1Ac tive = 1 Then
                                   Exit Sub
                               End If
                    >>
                    >Your statement says to execut the Exit Sub only if all three
                    >conditions are met.  In your example the first one is not met and the
                    >other two are.
                    >>
                    >Apparently you want Or instead of And.  An Or test is true if either
                    >item is true, and And test is true of both items are true.
                    >>
                    >Also, you should use AndAlso and OrElse instead of And and Or for
                    >tests like this.
                    >
                    >
                    >Ok a step further lets say:
                    >boolDSIFlushGa pReading = true and MuxClass.DSIVal ues.count =1 and my
                    >muxclass.COM1A ctive =2 it will not exit the program???
                    >
                    >How can I fix that.......
                    >
                    I don't understand what behavior you want.

                    In the example above, it would not exit because only one of the
                    conditions is true, and when you use And all must be true.

                    If you use Or, then if any condition is true it will exit, so in your
                    example above if you changed the Ands to Ors (or preferrably OrElse)
                    it would exit since muxClass.DSIVal ues.Count is < 2.

                    Comment

                    • Cor Ligthert[MVP]

                      #11
                      Re: If statement not catching on conditions

                      Why are you not reading what everybody writes, but does not want to give you
                      the fish but tries to teach you how to catch the fish.

                      Here is the fish

                      \\\
                      If boolDSIFlushGap Reading = False OrElse muxClass.DSIVal ues.Count < 2 OrElse
                      muxClass.COM1Ac tive <1 Then
                      Exit Sub
                      End If
                      ///

                      This means that if any of this tests is true, then it will Exit Sub

                      Do they all have to be true then it is

                      \\\
                      If boolDSIFlushGap Reading = False AndAlso muxClass.DSIVal ues.Count < 2
                      AndAlso
                      muxClass.COM1Ac tive <1 Then
                      Exit Sub
                      End If
                      ///


                      Cor

                      "cmdolcet69 " <colin_dolcetti @hotmail.comwro te in message
                      news:0f75e370-5f51-4928-8d6a-ef2a3be548d0@m3 2g2000hsf.googl egroups.com...
                      >I have the below if statement, that should catch if any of the
                      conditions are met.....however for some reasons if my
                      boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
                      muxclass.COM1Ac tive =1 it will not exit the program??? Why is that can
                      anyone help me
                      >
                      Urgent!!!!!!
                      >
                      >
                      >
                      If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
                      Not muxClass.COM1Ac tive = 1 Then
                      Exit Sub
                      End If

                      Comment

                      • Andrew Morton

                        #12
                        Re: If statement not catching on conditions

                        cmdolcet69 wrote:
                        I have the below if statement, that should catch if any of the
                        conditions are met.....however for some reasons if my
                        boolDSIFlushGap Reading = true and MuxClass.DSIVal ues.count =1 and my
                        muxclass.COM1Ac tive =1 it will not exit the program??? Why is that can
                        anyone help me
                        >
                        >
                        If boolDSIFlushGap Reading = False And muxClass.DSIVal ues.Count < 2 And
                        Not muxClass.COM1Ac tive = 1 Then
                        Exit Sub
                        End If
                        This is why everyone is saying to use "or" rather than "and": you're not
                        translating English into computer the right way round.

                        English: let's have all the apples and bananas->let's have all the apples
                        and let's have all the bananas

                        Computer: (all the items where x is an apple) + (all the items where x is a
                        banana)

                        Logically, the + translates to "or", not "and".


                        If in doubt, write it as three separate conditions to make it obvious:

                        if not(boolDSIFlus hGapReading) then
                        exit sub
                        end if

                        if MuxClass.DSIVal ues.count < 2 then
                        exit sub
                        end if

                        if muxClass.COM1Ac tive <1 then
                        exit sub
                        end if

                        Remember the syntax is "if <booleanthen... ", so you shouldn't test boolean
                        values: you should treat it directly as a boolean, which is why I changed
                        your "If boolDSIFlushGap Reading = False" to "if
                        not(boolDSIFlus hGapReading)".

                        Also, I think that "if muxClass.COM1Ac tive <1" is easier to read than "if
                        Not muxClass.COM1Ac tive = 1".

                        HTH

                        Andrew


                        Comment

                        • James Hahn

                          #13
                          Re: If statement not catching on conditions

                          "easier to read" is open to debate (although I don't disagree), but
                          if Not muxClass.COM1Ac tive = 1
                          relies on precedence, whereas
                          if muxClass.COM1Ac tive <1
                          does not. Barely significant in this example, but it can save a lot of
                          headscratching in more complex cases.

                          "Andrew Morton" <akm@in-press.co.uk.inv alidwrote in message
                          news:6mtheuFgs3 oeU1@mid.indivi dual.net...
                          snip <
                          >
                          Also, I think that "if muxClass.COM1Ac tive <1" is easier to read than
                          "if Not muxClass.COM1Ac tive = 1".
                          >
                          HTH
                          >
                          Andrew

                          Comment

                          • =?Utf-8?B?QmV0aA==?=

                            #14
                            Re: If statement not catching on conditions

                            I thought my threads were weird.

                            James, do you mean:
                            Not muxClass.COM1Ac tive = 1
                            is interpreted as:
                            (Not muxClass.COM1Ac tive) = 1
                            which would never be true, because true equates to -1, false to 0, and is
                            different from:
                            Not (muxClass.COM1A ctive = 1)

                            parens can be your friends...

                            maybe the whole problem is he should be testing for -1 instead of 1,
                            I don't know.

                            "James Hahn" wrote:
                            "easier to read" is open to debate (although I don't disagree), but
                            if Not muxClass.COM1Ac tive = 1
                            relies on precedence, whereas
                            if muxClass.COM1Ac tive <1
                            does not. Barely significant in this example, but it can save a lot of
                            headscratching in more complex cases.
                            >
                            "Andrew Morton" <akm@in-press.co.uk.inv alidwrote in message
                            news:6mtheuFgs3 oeU1@mid.indivi dual.net...
                            snip <

                            Also, I think that "if muxClass.COM1Ac tive <1" is easier to read than
                            "if Not muxClass.COM1Ac tive = 1".

                            HTH

                            Andrew
                            >
                            >

                            Comment

                            • James Hahn

                              #15
                              Re: If statement not catching on conditions

                              No. The two expressions are equivalent. I was simply commenting that the
                              form
                              Not muxClass.COM1Ac tive = 1
                              relies on
                              muxClass.COM1Ac tive = 1
                              having a higher precedence than the Not. My preference is for having an
                              expression that doesn't rely on the default precedence rules, therefore I
                              would use either
                              Not (muxClass.COM1A ctive = 1)
                              which makes it explicit or (much simpler)
                              if muxClass.COM1Ac tive <1

                              So not only does it _look_ more sensible but there's some programming sense
                              behind the choice - just a tiny bit in this case, but possibly relevant in
                              more complex cases. The original code was complicated enough without the
                              possibility of misunderstandin g the implied precedence rules.

                              "Beth" <Beth@discussio ns.microsoft.co mwrote in message
                              news:3C334849-4EF0-4A61-9908-D874E6CEB060@mi crosoft.com...
                              >I thought my threads were weird.
                              >
                              James, do you mean:
                              Not muxClass.COM1Ac tive = 1
                              is interpreted as:
                              (Not muxClass.COM1Ac tive) = 1
                              which would never be true, because true equates to -1, false to 0, and is
                              different from:
                              Not (muxClass.COM1A ctive = 1)
                              >
                              parens can be your friends...
                              >
                              maybe the whole problem is he should be testing for -1 instead of 1,
                              I don't know.
                              >
                              "James Hahn" wrote:
                              >
                              >"easier to read" is open to debate (although I don't disagree), but
                              > if Not muxClass.COM1Ac tive = 1
                              >relies on precedence, whereas
                              > if muxClass.COM1Ac tive <1
                              >does not. Barely significant in this example, but it can save a lot of
                              >headscratchi ng in more complex cases.
                              >>
                              >"Andrew Morton" <akm@in-press.co.uk.inv alidwrote in message
                              >news:6mtheuFgs 3oeU1@mid.indiv idual.net...
                              snip <
                              >
                              Also, I think that "if muxClass.COM1Ac tive <1" is easier to read than
                              "if Not muxClass.COM1Ac tive = 1".
                              >
                              HTH
                              >
                              Andrew
                              >>
                              >>

                              Comment

                              Working...