If ... Else, Operator problem

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

    #1

    If ... Else, Operator problem

    I have following code lines:

    =============== =============== =
    if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
    {
    txtMethod.Clear ();
    txtMethod.Focus ();
    }
    else
    {
    grpboxDirect.Vi sible = true;
    txtCalculatePer cent.Focus();
    }
    =============== ===============

    txtMethod is a TextBox that need to have values only D or F. The first
    line is giving problem. Please correct.

  • Chris Shepherd

    #2
    Re: If ... Else, Operator problem

    RP wrote:
    [...]
    You are missing an opening bracket:
    if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
    should be:
    if ((txtMethod.Tex t != "D") || (txtMethod.Text != "F"))
    Chris.

    Comment

    • mpetrotta@gmail.com

      #3
      Re: If ... Else, Operator problem

      On Aug 29, 9:32 am, RP <rpk.gene...@gm ail.comwrote:
      I have following code lines:
      >
      =============== =============== =
      if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
      {
      txtMethod.Clear ();
      txtMethod.Focus ();
      }
      else
      {
      grpboxDirect.Vi sible = true;
      txtCalculatePer cent.Focus();
      }
      =============== ===============
      >
      txtMethod is a TextBox that need to have values only D or F. The first
      line is giving problem. Please correct.
      Well, you didn't specify what problem you're having, but the code
      above won't compile - you're missing an open parenthesis. Also, your
      logic doesn't match what you say you want. You likely want:

      // "it's not a D, and it's not an F"
      if ((txtMethod.Tex t != "D") && (txtMethod.Text != "F"))

      You should consider refactoring to remove the negation; it tends to
      make code harder to read.

      if ((txtMethod.Tex t == "D") || (txtMethod.Text == "F"))
      {
      grpboxDirect.Vi sible = true;
      txtCalculatePer cent.Focus();
      }
      else
      {
      txtMethod.Clear ();
      txtMethod.Focus ();
      }

      Michael

      Comment

      • RP

        #4
        Re: If ... Else, Operator problem

        I corrected the parenthesis problem. Still I have similar code blocks
        where ELSE is not needed.
        So, something like:

        if ((txtMethod.Tex t == "D") || (txtMethod.Text == "F"))

        will not work. I have written this on TextChange event. If the text
        type is not D or not F then clear it, else do the ELSE part.
        Well, you didn't specify what problem you're having, but the code
        above won't compile - you're missing an open parenthesis. Also, your
        logic doesn't match what you say you want. You likely want:
        >
        // "it's not a D, and it's not an F"
        if ((txtMethod.Tex t != "D") && (txtMethod.Text != "F"))
        >
        You should consider refactoring to remove the negation; it tends to
        make code harder to read.
        >
        if ((txtMethod.Tex t == "D") || (txtMethod.Text == "F"))
        {
        grpboxDirect.Vi sible = true;
        txtCalculatePer cent.Focus();}
        >
        else
        {
        txtMethod.Clear ();
        txtMethod.Focus ();
        >
        }
        >
        Michael

        Comment

        • zacks@construction-imaging.com

          #5
          Re: If ... Else, Operator problem

          On Aug 29, 1:32 pm, RP <rpk.gene...@gm ail.comwrote:
          I have following code lines:
          >
          =============== =============== =
          if (txtMethod.Text != "D") || (txtMethod.Text != "F"))
          {
          txtMethod.Clear ();
          txtMethod.Focus ();
          }
          else
          {
          grpboxDirect.Vi sible = true;
          txtCalculatePer cent.Focus();
          }
          =============== ===============
          >
          txtMethod is a TextBox that need to have values only D or F. The first
          line is giving problem. Please correct.
          As another posted indicated, you should not combine NOTs with ORs.
          You'll get in trouble every time.

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: If ... Else, Operator problem

            RP <rpk.general@gm ail.comwrote:
            I corrected the parenthesis problem. Still I have similar code blocks
            where ELSE is not needed.
            So, something like:
            >
            if ((txtMethod.Tex t == "D") || (txtMethod.Text == "F"))
            >
            will not work. I have written this on TextChange event. If the text
            type is not D or not F then clear it, else do the ELSE part.
            I don't see why that wouldn't work.

            Could you post a short but complete program which demonstrates the
            problem?

            See http://www.pobox.com/~skeet/csharp/complete.html for details of
            what I mean by that.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: If ... Else, Operator problem

              <zacks@construc tion-imaging.comwrot e:
              As another posted indicated, you should not combine NOTs with ORs.
              You'll get in trouble every time.
              Not necessarily. In this particular case the two "nots" are exclusive
              (the text will always either be "not D" or "not F") but that's not
              always the case.

              Counter-example:

              if (!user.IsAuthen ticated || !user.IsAuthori zed)
              {
              // Display login page
              }

              that's effectively:

              if (!(user.IsAuthe nticated && user.IsAuthoriz ed))

              It makes perfect sense, and there's no "getting in trouble".

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • mpetrotta@gmail.com

                #8
                Re: If ... Else, Operator problem

                On Aug 29, 9:52 am, RP <rpk.gene...@gm ail.comwrote:
                I corrected the parenthesis problem. Still I have similar code blocks
                where ELSE is not needed.
                So, something like:
                >
                if ((txtMethod.Tex t == "D") || (txtMethod.Text == "F"))
                >
                will not work. I have written this on TextChange event. If the text
                type is not D or not F then clear it, else do the ELSE part.
                No. The condition you've stated ("if the text is not D or not F")
                will not work. Think about it; say you've got:
                if ((txtMethod.Tex t != "D") || (txtMethod.Text != "F"))
                You want that to resolve to false when txtMethod is "D", for
                instance. What you'll actually see is:
                if (( "D != "D") || ("D" != "F"))
                which resolves to:
                if ( false || true)
                which resolves to true.

                See my earlier reply for what you likely want to do. In general,
                though, it's worthwhile to "run" your algorithms in your mind with
                test cases, to see if they do the right thing. Also, while English
                often treats "and" and "or" as equivalent, boolean algebra definitely
                does not.

                Michael

                Comment

                • zacks@construction-imaging.com

                  #9
                  Re: If ... Else, Operator problem

                  On Aug 29, 2:11 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
                  <za...@construc tion-imaging.comwrot e:
                  As another posted indicated, you should not combine NOTs with ORs.
                  You'll get in trouble every time.
                  >
                  Not necessarily. In this particular case the two "nots" are exclusive
                  (the text will always either be "not D" or "not F") but that's not
                  always the case.
                  >
                  Counter-example:
                  >
                  if (!user.IsAuthen ticated || !user.IsAuthori zed)
                  {
                  // Display login page
                  >
                  }
                  >
                  that's effectively:
                  >
                  if (!(user.IsAuthe nticated && user.IsAuthoriz ed))
                  >
                  It makes perfect sense, and there's no "getting in trouble".
                  Let me re-phrase. When you try to mix NOTs with ORs, you better know
                  what you are doing. :-)

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: If ... Else, Operator problem

                    <zacks@construc tion-imaging.comwrot e:
                    It makes perfect sense, and there's no "getting in trouble".
                    >
                    Let me re-phrase. When you try to mix NOTs with ORs, you better know
                    what you are doing. :-)
                    You need to be careful - that's always the case, of course.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    Working...