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 to IF?!

how to IF?!

Scheduled Pinned Locked Moved C#
questiontutorial
31 Posts 7 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.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    I have two textbox BP_S and BP_D how can I make sure if one textbox is not null then the other should not be null as well... && and || won't worked I guess..

    L L A N 4 Replies Last reply
    0
    • J Jassim Rahma

      I have two textbox BP_S and BP_D how can I make sure if one textbox is not null then the other should not be null as well... && and || won't worked I guess..

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

      There is no room for guessing in software development.

      bool isBad = text1!="" && text2==""

      [CORRECTION: TextBox.Text never is null, it is an empty or a non-empty string] :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


      modified on Friday, April 9, 2010 6:17 PM

      J M 2 Replies Last reply
      0
      • J Jassim Rahma

        I have two textbox BP_S and BP_D how can I make sure if one textbox is not null then the other should not be null as well... && and || won't worked I guess..

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Why are your textboxes null to begin with? How about if (BP_S != null && BP_D != null)? Or did you mean that it is ok for the second textbox to be null as long as the first textbox is null as well: if (BP_D != null || BP_S == null)? (this is not help, btw, it's a bunch of questions)

        J 1 Reply Last reply
        0
        • L Luc Pattyn

          There is no room for guessing in software development.

          bool isBad = text1!="" && text2==""

          [CORRECTION: TextBox.Text never is null, it is an empty or a non-empty string] :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


          modified on Friday, April 9, 2010 6:17 PM

          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #4

          There is no room for guessing in software development.

          I have to mark it.. you are right...

          1 Reply Last reply
          0
          • L Lost User

            Why are your textboxes null to begin with? How about if (BP_S != null && BP_D != null)? Or did you mean that it is ok for the second textbox to be null as long as the first textbox is null as well: if (BP_D != null || BP_S == null)? (this is not help, btw, it's a bunch of questions)

            J Offline
            J Offline
            Jassim Rahma
            wrote on last edited by
            #5

            no i mean: if the user entered BP_S then he MUST enter BP_D similarly, if the user entered BP_D then he MUST enter BP_S but user can leave both BP_S and BP_D as NULL

            L 1 Reply Last reply
            0
            • J Jassim Rahma

              no i mean: if the user entered BP_S then he MUST enter BP_D similarly, if the user entered BP_D then he MUST enter BP_S but user can leave both BP_S and BP_D as NULL

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Ok so more like this: if (BP_S.Text.Length == 0 || BP_D.Text.Length != 0)?

              J 1 Reply Last reply
              0
              • L Luc Pattyn

                There is no room for guessing in software development.

                bool isBad = text1!="" && text2==""

                [CORRECTION: TextBox.Text never is null, it is an empty or a non-empty string] :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


                modified on Friday, April 9, 2010 6:17 PM

                M Offline
                M Offline
                Migounette
                wrote on last edited by
                #7

                I prefer this for String type: bool isBad = ((String.IsNullOrEmpty(text1) == true) || (String.IsNullOrEmpty(text2))) My 2 cents

                1 Reply Last reply
                0
                • L Lost User

                  Ok so more like this: if (BP_S.Text.Length == 0 || BP_D.Text.Length != 0)?

                  J Offline
                  J Offline
                  Jassim Rahma
                  wrote on last edited by
                  #8

                  yes but this code will force the user to enter one of the textbox.. What if the user wants to leave the null?

                  L 1 Reply Last reply
                  0
                  • J Jassim Rahma

                    yes but this code will force the user to enter one of the textbox.. What if the user wants to leave the null?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Ok it's quite late here and I'm tired so I could be wrong, but I'm fairly certain that if they are both null, that condition will evaluate to true

                    J 1 Reply Last reply
                    0
                    • L Lost User

                      Ok it's quite late here and I'm tired so I could be wrong, but I'm fairly certain that if they are both null, that condition will evaluate to true

                      J Offline
                      J Offline
                      Jassim Rahma
                      wrote on last edited by
                      #10

                      it's ok.. rest now :) I will also sleep and come back later... it's 1:30am here in Bahrain.. Sweet Dreamz :)

                      1 Reply Last reply
                      0
                      • J Jassim Rahma

                        I have two textbox BP_S and BP_D how can I make sure if one textbox is not null then the other should not be null as well... && and || won't worked I guess..

                        A Offline
                        A Offline
                        AspDotNetDev
                        wrote on last edited by
                        #11

                        Wow, this is probably the first time I've seen a valid use for the EXCLUSIVE OR operator:

                        if (!(string.IsNullOrEmpty(textBox1.Text) ^ string.IsNullOrEmpty(textBox2.Text)))
                        {
                        MessageBox.Show("Valid");
                        }
                        else
                        {
                        MessageBox.Show("Invalid");
                        }

                        You could also do that with && and ||, but this is probably the most succint technique.

                        [Forum Guidelines]

                        L L 2 Replies Last reply
                        0
                        • A AspDotNetDev

                          Wow, this is probably the first time I've seen a valid use for the EXCLUSIVE OR operator:

                          if (!(string.IsNullOrEmpty(textBox1.Text) ^ string.IsNullOrEmpty(textBox2.Text)))
                          {
                          MessageBox.Show("Valid");
                          }
                          else
                          {
                          MessageBox.Show("Invalid");
                          }

                          You could also do that with && and ||, but this is probably the most succint technique.

                          [Forum Guidelines]

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          aspdotnetdev wrote:

                          but this is probably the most succint technique

                          Maybe 2 or 3 characters shorter :)

                          1 Reply Last reply
                          0
                          • A AspDotNetDev

                            Wow, this is probably the first time I've seen a valid use for the EXCLUSIVE OR operator:

                            if (!(string.IsNullOrEmpty(textBox1.Text) ^ string.IsNullOrEmpty(textBox2.Text)))
                            {
                            MessageBox.Show("Valid");
                            }
                            else
                            {
                            MessageBox.Show("Invalid");
                            }

                            You could also do that with && and ||, but this is probably the most succint technique.

                            [Forum Guidelines]

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

                            most succinct? if exclusive OR is what the OP wants, then there is no need for XOR, OR, AND operators! Mind you, TextBox.Text never returns null.

                            MessageBox.Show((textBox1.Text.Length==0)==(textBox2.Text.Length==0)?"Valid":"Invalid");

                            :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                            Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


                            A 1 Reply Last reply
                            0
                            • J Jassim Rahma

                              I have two textbox BP_S and BP_D how can I make sure if one textbox is not null then the other should not be null as well... && and || won't worked I guess..

                              N Offline
                              N Offline
                              NavnathKale
                              wrote on last edited by
                              #14

                              :doh:

                              if((!String.IsNullOrEmpty(BP_S.Text) && !String.IsNullOrEmpty(BP_D.Text)) || (String.IsNullOrEmpty(BP_S.Text) && String.IsNullOrEmpty(BP_D.Text)))

                              Mark as answer if its really satisfies ur query !!!! :)

                              modified on Saturday, April 10, 2010 8:52 AM

                              L 1 Reply Last reply
                              0
                              • N NavnathKale

                                :doh:

                                if((!String.IsNullOrEmpty(BP_S.Text) && !String.IsNullOrEmpty(BP_D.Text)) || (String.IsNullOrEmpty(BP_S.Text) && String.IsNullOrEmpty(BP_D.Text)))

                                Mark as answer if its really satisfies ur query !!!! :)

                                modified on Saturday, April 10, 2010 8:52 AM

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                Now that's interesting, because that wouldn't allow you to fill in the second textbox if you didn't fill in the first as well - and he never said anything about that. Basically it's the same as aspdotnetdev's, but in the least-succinct way.

                                A 1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  most succinct? if exclusive OR is what the OP wants, then there is no need for XOR, OR, AND operators! Mind you, TextBox.Text never returns null.

                                  MessageBox.Show((textBox1.Text.Length==0)==(textBox2.Text.Length==0)?"Valid":"Invalid");

                                  :)

                                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                  Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


                                  A Offline
                                  A Offline
                                  AspDotNetDev
                                  wrote on last edited by
                                  #16

                                  You fool! It can be MUCH more succint!

                                  MessageBox.Show((textBox1.Text.Length>0)==(textBox2.Text.Length>0)?"Valid":"Invalid");

                                  [Forum Guidelines]

                                  L 1 Reply Last reply
                                  0
                                  • A AspDotNetDev

                                    You fool! It can be MUCH more succint!

                                    MessageBox.Show((textBox1.Text.Length>0)==(textBox2.Text.Length>0)?"Valid":"Invalid");

                                    [Forum Guidelines]

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

                                    Do you mean

                                    MessageBox.Show(((tb1.Text.Length>0)==(tb2.Text.Length>0)?"V":"Inv")+"alid");

                                    ? :)

                                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                    Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


                                    A 1 Reply Last reply
                                    0
                                    • L Luc Pattyn

                                      Do you mean

                                      MessageBox.Show(((tb1.Text.Length>0)==(tb2.Text.Length>0)?"V":"Inv")+"alid");

                                      ? :)

                                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                      Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


                                      A Offline
                                      A Offline
                                      AspDotNetDev
                                      wrote on last edited by
                                      #18

                                      Haha, I did exactly that, but then found it to take up more characters than the other method, to be less clear, and to potentially take up more processing time (the strig concatenation). If you make both words lowercase, you can get it down to exactly the same number of characters as the one I posted, but it still has the other issues. But nice try. ;)

                                      [Forum Guidelines]

                                      L 1 Reply Last reply
                                      0
                                      • A AspDotNetDev

                                        Haha, I did exactly that, but then found it to take up more characters than the other method, to be less clear, and to potentially take up more processing time (the strig concatenation). If you make both words lowercase, you can get it down to exactly the same number of characters as the one I posted, but it still has the other issues. But nice try. ;)

                                        [Forum Guidelines]

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

                                        correct.

                                        MessageBox.Show((tb1.Text.Length>0)==(tb2.Text.Length>0)?"OK":"!OK");

                                        :)

                                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                        Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


                                        A 1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          correct.

                                          MessageBox.Show((tb1.Text.Length>0)==(tb2.Text.Length>0)?"OK":"!OK");

                                          :)

                                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                          Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


                                          A Offline
                                          A Offline
                                          AspDotNetDev
                                          wrote on last edited by
                                          #20

                                          I had a nice long reply I was working on, but my Internet decided to go down just as I posted it. So, lucky for you, you get the short version instead:

                                          ((a.L>0)==(b.L>0)).S();

                                          ;P

                                          [Forum Guidelines]

                                          L 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