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. The Lounge
  3. PQOTD

PQOTD

Scheduled Pinned Locked Moved The Lounge
csharpquestion
59 Posts 31 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.
  • B BobJanova

    There's actually some nice concise uses of

    var result = DoSomething() && DoSomethingThatDependsOnIt();
    var result = DoSomething() || HandleFailureCase();

    It's even better in languages where you can use non-boolean types with the boolean operators.

    F Offline
    F Offline
    Forogar
    wrote on last edited by
    #27

    Ouch! && Ouch! || Ouch!

    - I would love to change the world, but they won’t give me the source code.

    1 Reply Last reply
    0
    • M Marc Clifton

      Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

      		bool more = ProcessFlyouts();
      		more = more || ProcessCarrierAnimations();
      

      How would you change it to "do the right thing?" Marc

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #28

      Now, I'm not going to look at the other answers before I post so I don't know if others have got it right, or if there are additional nuggets of information. If ProcessFlyouts returns true then the second test isn't carried out - it's a conditional OR statement. So, if you want ProcessCarrierAnimations to be evaluated, convert it to

      more = more | ProcessCarrierAnimations();

      1 Reply Last reply
      0
      • F Forogar

        This isn't a change from C++. It works the same way for both operators. Doesn't it? Now I'm going to have to read up on it, dammit!

        - I would love to change the world, but they won’t give me the source code.

        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu Peter
        wrote on last edited by
        #29

        You have to! Otherwise you may end up with some mess if updating C/C++ to C# (what I'm doing just now)...

        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

        F 1 Reply Last reply
        0
        • K Keith Barrow

          Though Nish's and OG's examples will work, I think it is symantically better to do this:

          bool more = ProcessFlyouts();
          bool foo = ProcessCarrierAnimations();
          more = more || foo;

          Less terse code (normally bad) - but explicit in calling ProcessCarrierAnimations.

          PB 369,783 wrote:

          I just find him very unlikeable, and I think the way he looks like a prettier version of his Mum is very disturbing.[^]

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #30

          "Eliminate needless local variables." :-D

          You'll never get very far if all you do is follow instructions.

          1 Reply Last reply
          0
          • R Rage

            MISRA[^] Rule 33 & 34:

            The right hand operand of a || shall not contain side effects
            The operands of a logical || shall be primary expressions

            The example you gave is the very reason for the above two rules.

            ~RaGE();

            I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Entropy isn't what it used to.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #31

            I never follow other peoples' rules. :cool:

            You'll never get very far if all you do is follow instructions.

            S 1 Reply Last reply
            0
            • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

              You have to! Otherwise you may end up with some mess if updating C/C++ to C# (what I'm doing just now)...

              I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

              F Offline
              F Offline
              Forogar
              wrote on last edited by
              #32

              Nah! As long as it compiles...

              - I would love to change the world, but they won’t give me the source code.

              Kornfeld Eliyahu PeterK 1 Reply Last reply
              0
              • F Forogar

                Nah! As long as it compiles...

                - I would love to change the world, but they won’t give me the source code.

                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu Peter
                wrote on last edited by
                #33

                Yeah! It compiles. After then you can rub your head to find where the bug is!

                I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                J 1 Reply Last reply
                0
                • M Marc Clifton

                  Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                  		bool more = ProcessFlyouts();
                  		more = more || ProcessCarrierAnimations();
                  

                  How would you change it to "do the right thing?" Marc

                  Richard Andrew x64R Offline
                  Richard Andrew x64R Offline
                  Richard Andrew x64
                  wrote on last edited by
                  #34

                  How about:

                  more &= ProcessCarrierAnimations();

                  The difficult we do right away... ...the impossible takes slightly longer.

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                    		bool more = ProcessFlyouts();
                    		more = more || ProcessCarrierAnimations();
                    

                    How would you change it to "do the right thing?" Marc

                    J Offline
                    J Offline
                    Jorgen Andersson
                    wrote on last edited by
                    #35

                    Is it just me that would have done it as a oneliner?

                    Wrong is evil and must be defeated. - Jeff Ello[^]

                    P 1 Reply Last reply
                    0
                    • J Jorgen Andersson

                      Is it just me that would have done it as a oneliner?

                      Wrong is evil and must be defeated. - Jeff Ello[^]

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #36

                      That could make setting a breakpoint on the second call difficult. :shrug:

                      You'll never get very far if all you do is follow instructions.

                      J 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        That could make setting a breakpoint on the second call difficult. :shrug:

                        You'll never get very far if all you do is follow instructions.

                        J Offline
                        J Offline
                        Jorgen Andersson
                        wrote on last edited by
                        #37

                        No big deal to change if necessary. :shrug: But I actually find it easier to read on one line.

                        Wrong is evil and must be defeated. - Jeff Ello[^]

                        1 Reply Last reply
                        0
                        • M Marc Clifton

                          Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                          		bool more = ProcessFlyouts();
                          		more = more || ProcessCarrierAnimations();
                          

                          How would you change it to "do the right thing?" Marc

                          E Offline
                          E Offline
                          Ennis Ray Lynch Jr
                          wrote on last edited by
                          #38

                          -|

                          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost "All users always want Excel" --Ennis Lynch

                          1 Reply Last reply
                          0
                          • M Marc Clifton

                            Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                            		bool more = ProcessFlyouts();
                            		more = more || ProcessCarrierAnimations();
                            

                            How would you change it to "do the right thing?" Marc

                            R Offline
                            R Offline
                            Ravi Bhavnani
                            wrote on last edited by
                            #39

                            There's a risk that ProcessCarrierAnimations() may never be invoked, so the right thing (IMHO) would be to do something like this:

                            bool processFlyouts = ProcessFlyouts();
                            bool processCarrierAnimations = ProcessCarrierAnimations();
                            bool more = processFlyouts || processCarrierAnimations;

                            /ravi

                            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                            1 Reply Last reply
                            0
                            • M Marc Clifton

                              Kornfeld Eliyahu Peter wrote:

                              you have to switch the order

                              Yes indeed. Or use the bitwise operator. Marc

                              D Offline
                              D Offline
                              dan sh
                              wrote on last edited by
                              #40

                              Marc Clifton wrote:

                              bitwise operator

                              So boolean is foolish?

                              My CP workspace: Incredibly trivial and probably useless code samples[^]

                              1 Reply Last reply
                              0
                              • M Marc Clifton

                                Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                                		bool more = ProcessFlyouts();
                                		more = more || ProcessCarrierAnimations();
                                

                                How would you change it to "do the right thing?" Marc

                                D Offline
                                D Offline
                                dan sh
                                wrote on last edited by
                                #41

                                No it did not do anything unexpected. || operator uses short-circuit evaluation. I would assume the intent was to call ProcessCarrierAnimations only if more was false.

                                My CP workspace: Incredibly trivial and probably useless code samples[^]

                                1 Reply Last reply
                                0
                                • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                                  Yeah! It compiles. After then you can rub your head to find where the bug is!

                                  I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                  J Offline
                                  J Offline
                                  JimmyRopes
                                  wrote on last edited by
                                  #42

                                  Kornfeld Eliyahu Peter wrote:

                                  It compiles. After then you someone else will can rub your their head to find where the bug is!

                                  FTFY :-D

                                  The report of my death was an exaggeration - Mark Twain
                                  Simply Elegant Designs JimmyRopes Designs
                                  I'm on-line therefore I am. JimmyRopes

                                  1 Reply Last reply
                                  0
                                  • M Marc Clifton

                                    Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                                    		bool more = ProcessFlyouts();
                                    		more = more || ProcessCarrierAnimations();
                                    

                                    How would you change it to "do the right thing?" Marc

                                    C Offline
                                    C Offline
                                    Chris Quinn
                                    wrote on last edited by
                                    #43

                                    It compiled?

                                    ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

                                    1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      I never follow other peoples' rules. :cool:

                                      You'll never get very far if all you do is follow instructions.

                                      S Offline
                                      S Offline
                                      SortaCore
                                      wrote on last edited by
                                      #44

                                      It's my rule that people have to breathe (or die trying).

                                      1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                                        		bool more = ProcessFlyouts();
                                        		more = more || ProcessCarrierAnimations();
                                        

                                        How would you change it to "do the right thing?" Marc

                                        A Offline
                                        A Offline
                                        Adam Tibi
                                        wrote on last edited by
                                        #45

                                        This is JavaScript style, the developer meant, if "more" is "undefined" then try the other option ProcessCarrierAnimations(). But obviously, this doesn't work in C#.

                                        Make it simple, as simple as possible, but not simpler.

                                        1 Reply Last reply
                                        0
                                        • M Marc Clifton

                                          Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                                          		bool more = ProcessFlyouts();
                                          		more = more || ProcessCarrierAnimations();
                                          

                                          How would you change it to "do the right thing?" Marc

                                          G Offline
                                          G Offline
                                          Gary Wheeler
                                          wrote on last edited by
                                          #46

                                          Just to be contrary:

                                          bool more = false;
                                          if (ProcessFlyouts()) more = true;
                                          if (ProcessCarrierAnimations()) more = true;

                                          Software Zen: delete this;

                                          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