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. Huh? Can this be logical?

Huh? Can this be logical?

Scheduled Pinned Locked Moved C#
csharpcomquestion
16 Posts 6 Posters 3 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.
  • L leppie

    Steven Campbell wrote: if (email = null), (note the single equal sign) and it will always evaluate to true. allways be false i think u mean :p top secret
    Download xacc-ide 0.0.3 now!
    See some screenshots

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #7

    I'd expect it to be true, because the assignment would not fail. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

    H 1 Reply Last reply
    0
    • S Steven Campbell

      This is a wierd convention that older C programmers follow. It is perfectly legal. The reason that it is done, is that in C, it is legal to say if (email = null), (note the single equal sign) and it will always evaluate to true. So, to prevent possible errors caused by mistypes, C programmers switch the arguments, i.e. if (null = email). This second version is not legal syntax, and will give a compiler error. The programmer can then fix it by changing it to if (null == email). This is not necessary in C# (but neither is it illegal), because the C# compiler will complain at if (email = null).


      my blog

      J Offline
      J Offline
      Jerry Hammond
      wrote on last edited by
      #8

      Steven Campbell wrote: The programmer can then fix it by changing it to if (null == email). Ok, but how can

      null

      be "equal to" anything? This has got to be a concept I'm not able to fathom or I am just unfamiliar with it. And, for now, it just seem logical. Best, Jerry

      Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes

      Toasty0.com

      H C 2 Replies Last reply
      0
      • J Jerry Hammond

        Steven Campbell wrote: The programmer can then fix it by changing it to if (null == email). Ok, but how can

        null

        be "equal to" anything? This has got to be a concept I'm not able to fathom or I am just unfamiliar with it. And, for now, it just seem logical. Best, Jerry

        Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes

        Toasty0.com

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #9

        Any reference object - like email (presumably a string, which is a reference type) - is null if it doesn't reference an object. So, it's "value" is null.

        string email = null;
        null == email; // Returns true
        email = "you@codeproject.com";
        null == email; // Returns false, references the string "you@codeproject.com

        This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

        1 Reply Last reply
        0
        • C Christian Graus

          I'd expect it to be true, because the assignment would not fail. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #10

          The condition isn't checking the assignment, it would check the resultant expression. In ths case, that'd be email. The code breaks apart like so:

          email = null;
          if (email)
          {
          // ...
          }

          In C#, however, the above expression isn't valid like it would be in C/C++ (where NULL is defined as 0). You would get an error in this case, but not because you'd be using an assignment operator instead of an equivalence operator. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

          C 1 Reply Last reply
          0
          • H Heath Stewart

            The condition isn't checking the assignment, it would check the resultant expression. In ths case, that'd be email. The code breaks apart like so:

            email = null;
            if (email)
            {
            // ...
            }

            In C#, however, the above expression isn't valid like it would be in C/C++ (where NULL is defined as 0). You would get an error in this case, but not because you'd be using an assignment operator instead of an equivalence operator. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #11

            Oh, OK. Obviously I had not tried it. Heath Stewart wrote: Software Design Engineer Developer Division Sustained Engineering Microsoft Cool. Do you know anything about DX9, specifically the AudioVideoPlayback namespace ? Was it released by mistake ? I've had a lot of fun figuring out it's many flaws..... Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

            H 1 Reply Last reply
            0
            • J Jerry Hammond

              Steven Campbell wrote: The programmer can then fix it by changing it to if (null == email). Ok, but how can

              null

              be "equal to" anything? This has got to be a concept I'm not able to fathom or I am just unfamiliar with it. And, for now, it just seem logical. Best, Jerry

              Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes

              Toasty0.com

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #12

              if 2+2 = 4, then 4 = 2+2. The concept is the same. The two values are checked for equality, that the constant value is on the left makes no difference to the compiler, and in C++ was a good was to avoid a bug that can be a nightmare to find. I know, I still sometimes write code like this, a habit I formed in my C++ days. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

              J 1 Reply Last reply
              0
              • C Christian Graus

                Oh, OK. Obviously I had not tried it. Heath Stewart wrote: Software Design Engineer Developer Division Sustained Engineering Microsoft Cool. Do you know anything about DX9, specifically the AudioVideoPlayback namespace ? Was it released by mistake ? I've had a lot of fun figuring out it's many flaws..... Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #13

                I've worked with DX9 a little, but I admit I don't know a whole lot about it (mostly from a graphics manipulation regard, not API use). I recommend reading Managed DirectX 9 Kick Start : Graphics and Game Programming[^]. It's a very good book and you'd probably have a better time with it. Like I said, I understand all the API stuff, but matrices - when it comes to graphics - always seem to elude me. I will say, however, that you're articles have helped clear a few things up. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

                C 1 Reply Last reply
                0
                • C Christian Graus

                  if 2+2 = 4, then 4 = 2+2. The concept is the same. The two values are checked for equality, that the constant value is on the left makes no difference to the compiler, and in C++ was a good was to avoid a bug that can be a nightmare to find. I know, I still sometimes write code like this, a habit I formed in my C++ days. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

                  J Offline
                  J Offline
                  Jerry Hammond
                  wrote on last edited by
                  #14

                  Ok, thanks Heath and Christian. I think I understand what's going on with that now. Best, while(null==jerry) { null++; return; }

                  Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes

                  Toasty0.com

                  1 Reply Last reply
                  0
                  • H Heath Stewart

                    I've worked with DX9 a little, but I admit I don't know a whole lot about it (mostly from a graphics manipulation regard, not API use). I recommend reading Managed DirectX 9 Kick Start : Graphics and Game Programming[^]. It's a very good book and you'd probably have a better time with it. Like I said, I understand all the API stuff, but matrices - when it comes to graphics - always seem to elude me. I will say, however, that you're articles have helped clear a few things up. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #15

                    Heath Stewart wrote: I recommend reading Managed DirectX 9 Kick Start : Graphics and Game Programming[^]. I have it. I assume the rest of the book is better, the bit on audio video playback is 3 pages which glosses over the API, tells me that Microsoft does not regard it as a full implimentation, and it's not going to be updated. It doesn't cover any of the bugs in the API, I presume the author has never actually used this part of DirectX9, and felt they needed to cover it, so they skimmed the docs. I'm writing an article on all the ways that the AudioVideoPlayback namespace fails to perform as expected, and a wrapper class that will make it a little bit close to usable. I've built an entire project around DX9, and I'm kind of wishing I hadn't. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

                    1 Reply Last reply
                    0
                    • J Jerry Hammond

                      Looking for some answers on MSDN I ran across the following code. If (null != email) Is that legal in C#, and even if it does compile, is it logical? Shouldn't it more likely read: If (email != null) Best, Jerry

                      Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes

                      Toasty0.com

                      E Offline
                      E Offline
                      Eric Gunnerson msft
                      wrote on last edited by
                      #16

                      In C++, it's possible to write: if (x = 0) when you mean to write if (x == 0) Because of that, some developers invert the order of what they write, as: if (0 = x) is illegal, while if (0 == x) is legal. This is not a problem in C#, where if (x = 0) is prohibited.

                      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