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. C#
  4. How come this If sentence doesnt work?

How come this If sentence doesnt work?

Scheduled Pinned Locked Moved C#
question
15 Posts 9 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.
  • M Matjaz xyz

    I cant get it right... why this doesnt work :S:S

    string b = "3.12312123";
    if ((b[0] == '2' || b[0] == '3' || b[0] == '4' || b[0] == '5') && ((textBox2.Text != "703") || (textBox2.Text != "733")))
    {
    this.Text = "OK";
    }
    else
    {
    this.Text = "NOT OK!";

            }
    

    Any ideas?

    Regards, Matjaž

    G Offline
    G Offline
    Gideon Engelberth
    wrote on last edited by
    #2

    I can't be sure what you mean by not working, but that condition will always be true because of this: ((textBox2.Text != "703") || (textBox2.Text != "733")) The textBox2 text will never be equal to both 703 and 733. As an aside, you seem to be doing number comparisons, so I would recommend using number variables instead of strings. You could then test (b >= 2 && b < 6)

    M 1 Reply Last reply
    0
    • M Matjaz xyz

      I cant get it right... why this doesnt work :S:S

      string b = "3.12312123";
      if ((b[0] == '2' || b[0] == '3' || b[0] == '4' || b[0] == '5') && ((textBox2.Text != "703") || (textBox2.Text != "733")))
      {
      this.Text = "OK";
      }
      else
      {
      this.Text = "NOT OK!";

              }
      

      Any ideas?

      Regards, Matjaž

      L Offline
      L Offline
      Lev Danielyan
      wrote on last edited by
      #3

      This code

      ((textBox2.Text != "703") || (textBox2.Text != "733"))

      will always be true

      Regards, Lev

      1 Reply Last reply
      0
      • M Matjaz xyz

        I cant get it right... why this doesnt work :S:S

        string b = "3.12312123";
        if ((b[0] == '2' || b[0] == '3' || b[0] == '4' || b[0] == '5') && ((textBox2.Text != "703") || (textBox2.Text != "733")))
        {
        this.Text = "OK";
        }
        else
        {
        this.Text = "NOT OK!";

                }
        

        Any ideas?

        Regards, Matjaž

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #4

        well, ((textBox2.Text != "703") || (textBox2.Text != "733")) will always evaluate to true for starters.

        Regards, Rob Philpott.

        1 Reply Last reply
        0
        • M Matjaz xyz

          I cant get it right... why this doesnt work :S:S

          string b = "3.12312123";
          if ((b[0] == '2' || b[0] == '3' || b[0] == '4' || b[0] == '5') && ((textBox2.Text != "703") || (textBox2.Text != "733")))
          {
          this.Text = "OK";
          }
          else
          {
          this.Text = "NOT OK!";

                  }
          

          Any ideas?

          Regards, Matjaž

          M Offline
          M Offline
          Matjaz xyz
          wrote on last edited by
          #5

          [Message Deleted]

          L 1 Reply Last reply
          0
          • G Gideon Engelberth

            I can't be sure what you mean by not working, but that condition will always be true because of this: ((textBox2.Text != "703") || (textBox2.Text != "733")) The textBox2 text will never be equal to both 703 and 733. As an aside, you seem to be doing number comparisons, so I would recommend using number variables instead of strings. You could then test (b >= 2 && b < 6)

            M Offline
            M Offline
            Matjaz xyz
            wrote on last edited by
            #6

            Okeeei... So, i want to check... if its enetered only 703 or 733... BESIDES that the b starts with 2,3,4 or 5. I really dont know how to make this thing to work... maybe i jsut have a bad day and am a bit confused... but yet again... i dont understand it Please help? :/

            Regards, Matjaž

            P 1 Reply Last reply
            0
            • M Matjaz xyz

              [Message Deleted]

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #7

              Hi, if you don't understand why a statement behaves the way it does, then the only good advice is to write simpler statements. Why do you insist on putting everything in a single line? Chop it into smaller pieces and look at the intermediate values, that will help you understand if and how it works. As for the 703/733 thing, as all the others already told you, that cannot possibly be what you intended. For what value do you think it will evaluate to false? :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Fixturized forever. :confused:


              M 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, if you don't understand why a statement behaves the way it does, then the only good advice is to write simpler statements. Why do you insist on putting everything in a single line? Chop it into smaller pieces and look at the intermediate values, that will help you understand if and how it works. As for the 703/733 thing, as all the others already told you, that cannot possibly be what you intended. For what value do you think it will evaluate to false? :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Fixturized forever. :confused:


                M Offline
                M Offline
                Matjaz xyz
                wrote on last edited by
                #8

                Well, my intention was ... If the first character of the string was 2,3,4 or 5 the it should also check the textbox if there is entered any of the two mentioned values (703 or 733). Ok, looking at it this way... maybe it could work with two ifs...

                Regards, Matjaž

                M 1 Reply Last reply
                0
                • M Matjaz xyz

                  Well, my intention was ... If the first character of the string was 2,3,4 or 5 the it should also check the textbox if there is entered any of the two mentioned values (703 or 733). Ok, looking at it this way... maybe it could work with two ifs...

                  Regards, Matjaž

                  M Offline
                  M Offline
                  Matjaz xyz
                  wrote on last edited by
                  #9
                          if (b\[0\] == '2' || b\[0\] == '3' || b\[0\] == '4' || b\[0\] == '5')
                              if((textBox2.Text != "703") || (textBox2.Text != "733")) 
                              {
                              this.Text = "OK";
                              }
                              else
                              {
                                  this.Text = "NI OK!";
                              }
                  

                  ok... this doesnt work either:S

                  Regards, Matjaž

                  D C 2 Replies Last reply
                  0
                  • M Matjaz xyz
                            if (b\[0\] == '2' || b\[0\] == '3' || b\[0\] == '4' || b\[0\] == '5')
                                if((textBox2.Text != "703") || (textBox2.Text != "733")) 
                                {
                                this.Text = "OK";
                                }
                                else
                                {
                                    this.Text = "NI OK!";
                                }
                    

                    ok... this doesnt work either:S

                    Regards, Matjaž

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #10

                    Matjaž Grahek wrote:

                    ((textBox2.Text != "703") || (textBox2.Text != "733"))

                    Guy, for the last time, this will ALWAYS evaluate to True. How about change that "or" to an "and"??

                    if ((textbox2.Text != "703") && (textbox2.Text != "733"))
                    

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    1 Reply Last reply
                    0
                    • M Matjaz xyz
                              if (b\[0\] == '2' || b\[0\] == '3' || b\[0\] == '4' || b\[0\] == '5')
                                  if((textBox2.Text != "703") || (textBox2.Text != "733")) 
                                  {
                                  this.Text = "OK";
                                  }
                                  else
                                  {
                                      this.Text = "NI OK!";
                                  }
                      

                      ok... this doesnt work either:S

                      Regards, Matjaž

                      C Offline
                      C Offline
                      carbon_golem
                      wrote on last edited by
                      #11

                      It doesn't work because you haven't changed the logic. Scott P.

                      "Simplicity carried to the extreme becomes elegance."
                      -Jon Franklin

                      1 Reply Last reply
                      0
                      • M Matjaz xyz

                        I cant get it right... why this doesnt work :S:S

                        string b = "3.12312123";
                        if ((b[0] == '2' || b[0] == '3' || b[0] == '4' || b[0] == '5') && ((textBox2.Text != "703") || (textBox2.Text != "733")))
                        {
                        this.Text = "OK";
                        }
                        else
                        {
                        this.Text = "NOT OK!";

                                }
                        

                        Any ideas?

                        Regards, Matjaž

                        C Offline
                        C Offline
                        carbon_golem
                        wrote on last edited by
                        #12

                        class Program { static void Main(string[] args) { string b = "3.12312123"; if ((b[0] == '2' || b[0] == '3' || b[0] == '4' || b[0] == '5') && ((args[0] == "703") || (args[0]== "733"))) { Console.WriteLine("OK"); } else { Console.WriteLine("NOT OK!"); } } }
                        Merry Christmas.

                        "Simplicity carried to the extreme becomes elegance."
                        -Jon Franklin

                        1 Reply Last reply
                        0
                        • M Matjaz xyz

                          Okeeei... So, i want to check... if its enetered only 703 or 733... BESIDES that the b starts with 2,3,4 or 5. I really dont know how to make this thing to work... maybe i jsut have a bad day and am a bit confused... but yet again... i dont understand it Please help? :/

                          Regards, Matjaž

                          P Offline
                          P Offline
                          Pr teek B h
                          wrote on last edited by
                          #13

                          Matjaž Grahek wrote:

                          i want to check... if its enetered only 703 or 733

                          I have no idea what the condition is. a) Do you want this.text = "OK" if text = 703 or 733? b) or do you want this.text = "Not Ok!" if text = 703 or 733?

                          int textVal = 0;
                          int.tryparse(textbox2.text, out textVal);

                          if(textVal == 703 || textVal==733)
                          {
                          this.Text = "ok"; // Case 'a'
                          this.Text = "Not Ok"; // Case 'b'
                          }

                          Matjaž Grahek wrote:

                          b starts with 2,3,4 or 5

                          here is a simple solution to that:

                          int val = 0;
                          int.tryparse(b[0].ToString(), out val)

                          this should help, Prateek

                          1 Reply Last reply
                          0
                          • M Matjaz xyz

                            I cant get it right... why this doesnt work :S:S

                            string b = "3.12312123";
                            if ((b[0] == '2' || b[0] == '3' || b[0] == '4' || b[0] == '5') && ((textBox2.Text != "703") || (textBox2.Text != "733")))
                            {
                            this.Text = "OK";
                            }
                            else
                            {
                            this.Text = "NOT OK!";

                                    }
                            

                            Any ideas?

                            Regards, Matjaž

                            L Offline
                            L Offline
                            LongRange Shooter
                            wrote on last edited by
                            #14

                            Your problem displays the most common failure of beginning programmers -- mixing their 'AND' and their 'OR' logic. You also need to think of how to address your other requirement that the text begin with 2,3,4, or 5. A possible solution is first to write what you want out in text if b starts with a valid number and textbox contains valid numbers then it's ok. That leads to code such as this:

                            string b = "3.12321212";

                            List<char> validValues = new List<char> { '2', '3', '4', '5' };
                            List<string> validInput = new List<string> { "703", "733" };

                            textbox1.Text = "Not OK";

                            if ( validValues.Contains( b[0] ) && validInput.Contains( texbox.Text ) )
                            {
                            textbox1.Text = "OK";
                            }

                            Another way you can also address this problem is with RegEx but I don't have the time to figure out the pattern.

                            1 Reply Last reply
                            0
                            • M Matjaz xyz

                              I cant get it right... why this doesnt work :S:S

                              string b = "3.12312123";
                              if ((b[0] == '2' || b[0] == '3' || b[0] == '4' || b[0] == '5') && ((textBox2.Text != "703") || (textBox2.Text != "733")))
                              {
                              this.Text = "OK";
                              }
                              else
                              {
                              this.Text = "NOT OK!";

                                      }
                              

                              Any ideas?

                              Regards, Matjaž

                              M Offline
                              M Offline
                              Matjaz xyz
                              wrote on last edited by
                              #15

                              Ok... thank you all for your answers. Will try this tommorow. Again, thanks for your patience and the will to help. Good night

                              Regards, Matjaž

                              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