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. Other Discussions
  3. The Weird and The Wonderful
  4. IIF to the rescue...

IIF to the rescue...

Scheduled Pinned Locked Moved The Weird and The Wonderful
help
13 Posts 6 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 Bernhard Hiller

    Where do you see the problem behind that code that justifies it to be posted in the Weird and Wonderful? Well, that IsNew function should better be a function of the Order class and not need a parameter (because OrderNumber could be expected to be a Property). But otherwise? Does VB.Net have the ternary operator like C# (return OrderNumber = 0 ? true : false;)? I don't know. Or did you want to tell us that it's better to do the comparison of OrderNumber to 0 whenever that IsNew function is used? Oh no! That would deserve to be posted here! IsNew clearly tells you the intention of the code, while If(OrderNumber=0) does not.

    Sander RosselS Offline
    Sander RosselS Offline
    Sander Rossel
    wrote on last edited by
    #3

    It is a function on the Order class, but it still takes a parameter (it just gets mOrderNumber given to it every time). VB does not have a ternary operator. I'm saying the function could look like this: Return OrderNumber = 0 There is no need for an IIF anywhere in the code. As I said, it's not truly horrible, but it's an 'interesting' way to return if OrderNumber = 0 :)

    It's an OO world.

    public class SanderRossel : Lazy<Person>
    {
    public void DoWork()
    {
    throw new NotSupportedException();
    }
    }

    B N P 3 Replies Last reply
    0
    • Sander RosselS Sander Rossel

      It is a function on the Order class, but it still takes a parameter (it just gets mOrderNumber given to it every time). VB does not have a ternary operator. I'm saying the function could look like this: Return OrderNumber = 0 There is no need for an IIF anywhere in the code. As I said, it's not truly horrible, but it's an 'interesting' way to return if OrderNumber = 0 :)

      It's an OO world.

      public class SanderRossel : Lazy<Person>
      {
      public void DoWork()
      {
      throw new NotSupportedException();
      }
      }

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #4

      Ah, thanks. That explains it. It really deserves this place.

      Sander RosselS 1 Reply Last reply
      0
      • B Bernhard Hiller

        Ah, thanks. That explains it. It really deserves this place.

        Sander RosselS Offline
        Sander RosselS Offline
        Sander Rossel
        wrote on last edited by
        #5

        Don't worry, we all have our off-days :laugh:

        It's an OO world.

        public class SanderRossel : Lazy<Person>
        {
        public void DoWork()
        {
        throw new NotSupportedException();
        }
        }

        B 1 Reply Last reply
        0
        • Sander RosselS Sander Rossel

          It is a function on the Order class, but it still takes a parameter (it just gets mOrderNumber given to it every time). VB does not have a ternary operator. I'm saying the function could look like this: Return OrderNumber = 0 There is no need for an IIF anywhere in the code. As I said, it's not truly horrible, but it's an 'interesting' way to return if OrderNumber = 0 :)

          It's an OO world.

          public class SanderRossel : Lazy<Person>
          {
          public void DoWork()
          {
          throw new NotSupportedException();
          }
          }

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #6

          Sander Rossel wrote:

          VB does not have a ternary operator.

          It does, it's the IIf function/method/whateva

          Sander RosselS Richard DeemingR 2 Replies Last reply
          0
          • N Nagy Vilmos

            Sander Rossel wrote:

            VB does not have a ternary operator.

            It does, it's the IIf function/method/whateva

            Sander RosselS Offline
            Sander RosselS Offline
            Sander Rossel
            wrote on last edited by
            #7

            Is that an operator or a function? ;) I wouldn't know the difference, but I believe there is one. Something like an operator is a function, but a function is not an operator.

            It's an OO world.

            public class SanderRossel : Lazy<Person>
            {
            public void DoWork()
            {
            throw new NotSupportedException();
            }
            }

            1 Reply Last reply
            0
            • N Nagy Vilmos

              Sander Rossel wrote:

              VB does not have a ternary operator.

              It does, it's the IIf function/method/whateva

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #8

              Not quite. IIf is a function[^], and as such it evaluates both arguments before checking the condition. So while this works in C#:

              someObject == null ? "Null" : someObject.ToString()

              the equivalent using IIf will throw a NullReferenceException:

              IIf(someObject Is Nothing, "Null", someObject.ToString()) ' Boom!

              The real ternary operator for VB.NET is the If operator[^], which was added in .NET 3.5:

              If(someObject Is Nothing, "Null", someObject.ToString()) ' No Boom.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • Sander RosselS Sander Rossel

                Don't worry, we all have our off-days :laugh:

                It's an OO world.

                public class SanderRossel : Lazy<Person>
                {
                public void DoWork()
                {
                throw new NotSupportedException();
                }
                }

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #9

                I guess it was an unhandled ConfusedByVBException...

                Sander RosselS 1 Reply Last reply
                0
                • B Bernhard Hiller

                  I guess it was an unhandled ConfusedByVBException...

                  Sander RosselS Offline
                  Sander RosselS Offline
                  Sander Rossel
                  wrote on last edited by
                  #10

                  :laugh:

                  It's an OO world.

                  public class SanderRossel : Lazy<Person>
                  {
                  public void DoWork()
                  {
                  throw new NotSupportedException();
                  }
                  }

                  1 Reply Last reply
                  0
                  • Sander RosselS Sander Rossel

                    It is a function on the Order class, but it still takes a parameter (it just gets mOrderNumber given to it every time). VB does not have a ternary operator. I'm saying the function could look like this: Return OrderNumber = 0 There is no need for an IIF anywhere in the code. As I said, it's not truly horrible, but it's an 'interesting' way to return if OrderNumber = 0 :)

                    It's an OO world.

                    public class SanderRossel : Lazy<Person>
                    {
                    public void DoWork()
                    {
                    throw new NotSupportedException();
                    }
                    }

                    P Offline
                    P Offline
                    PaulLinton
                    wrote on last edited by
                    #11

                    I don't know VB and was just wondering ... if

                    Return OrderNumber = 0

                    does a comparison and returns the result then how would you set the value of OrderNumber to 0 and then return OrderNumber? In C#, the first is

                    return OrderNumber == 0;

                    and the second is

                    return OrderNumber = 0;

                    Sander RosselS 1 Reply Last reply
                    0
                    • P PaulLinton

                      I don't know VB and was just wondering ... if

                      Return OrderNumber = 0

                      does a comparison and returns the result then how would you set the value of OrderNumber to 0 and then return OrderNumber? In C#, the first is

                      return OrderNumber == 0;

                      and the second is

                      return OrderNumber = 0;

                      Sander RosselS Offline
                      Sander RosselS Offline
                      Sander Rossel
                      wrote on last edited by
                      #12

                      PaulLinton wrote:

                      I don't know VB

                      Obviously :) VB doesn't know the == operator. Instead = can be either assignment or comparison, dependent on context. In this case it's a comparison, but no assignment. So

                      Return OrderNumber = 0

                      Would be

                      return OrderNumber == 0;

                      in C#. Your second C# example isn't possible in VB (as far as I know, but I didn't know it was possible in C# either :) )

                      It's an OO world.

                      public class SanderRossel : Lazy<Person>
                      {
                      public void DoWork()
                      {
                      throw new NotSupportedException();
                      }
                      }

                      J 1 Reply Last reply
                      0
                      • Sander RosselS Sander Rossel

                        PaulLinton wrote:

                        I don't know VB

                        Obviously :) VB doesn't know the == operator. Instead = can be either assignment or comparison, dependent on context. In this case it's a comparison, but no assignment. So

                        Return OrderNumber = 0

                        Would be

                        return OrderNumber == 0;

                        in C#. Your second C# example isn't possible in VB (as far as I know, but I didn't know it was possible in C# either :) )

                        It's an OO world.

                        public class SanderRossel : Lazy<Person>
                        {
                        public void DoWork()
                        {
                        throw new NotSupportedException();
                        }
                        }

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

                        The second one would be:

                        Return (Ordernumber = 0)

                        <edit>Or like this:

                        Return 0 = Ordernumber

                        </edit>

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

                        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