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. use of ? and : [Solved]

use of ? and : [Solved]

Scheduled Pinned Locked Moved C#
question
6 Posts 4 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.
  • W Offline
    W Offline
    William Winner
    wrote on last edited by
    #1

    I'm sure to some of you, this is pretty simple, and I think I know what it does, but I haven't been able to find the answer on the net. What exactly do the '?' and ':' mean in this bit of code?

    (dssubcat.Tables[0].Rows[0]["gallery_img"]) == System.DBNull.Value ? "" : dssubcat.Tables[0].Rows[0]["gallery_img"])

    Does that line say that if that item in the DataSet is equal to the DBNull value, then return "" and if it doesn't return the item?

    modified on Saturday, May 22, 2010 5:30 PM

    P D 2 Replies Last reply
    0
    • W William Winner

      I'm sure to some of you, this is pretty simple, and I think I know what it does, but I haven't been able to find the answer on the net. What exactly do the '?' and ':' mean in this bit of code?

      (dssubcat.Tables[0].Rows[0]["gallery_img"]) == System.DBNull.Value ? "" : dssubcat.Tables[0].Rows[0]["gallery_img"])

      Does that line say that if that item in the DataSet is equal to the DBNull value, then return "" and if it doesn't return the item?

      modified on Saturday, May 22, 2010 5:30 PM

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

      This is known as the ternary operator, and you are right about what your assumption of the logic here is. Basically, it follows the pattern: (some boolean test) ? (part that gets executed if true) : (part that gets executed if false). It's very easy to get carried away with this operator and produce some code that looks like this:

      value < lowerBounds ? value > 0 ? Console.WriteLine("We have a problem") : Console.WriteLine("Value less than or equal to zero") : Console.WriteLine("The value is OK");

      If you must use it, use it with care; at some point you may be responsible for trying to figure out what you were smoking when you wrote the code.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      W R 2 Replies Last reply
      0
      • P Pete OHanlon

        This is known as the ternary operator, and you are right about what your assumption of the logic here is. Basically, it follows the pattern: (some boolean test) ? (part that gets executed if true) : (part that gets executed if false). It's very easy to get carried away with this operator and produce some code that looks like this:

        value < lowerBounds ? value > 0 ? Console.WriteLine("We have a problem") : Console.WriteLine("Value less than or equal to zero") : Console.WriteLine("The value is OK");

        If you must use it, use it with care; at some point you may be responsible for trying to figure out what you were smoking when you wrote the code.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        W Offline
        W Offline
        William Winner
        wrote on last edited by
        #3

        Honestly, I'm never that concerned with lessening the number of lines or getting rid of a few extra key strokes that I would probably ever use it. I just saw it in a question and wanted to make sure I understood for sure what it was. Thanks!

        P 1 Reply Last reply
        0
        • W William Winner

          Honestly, I'm never that concerned with lessening the number of lines or getting rid of a few extra key strokes that I would probably ever use it. I just saw it in a question and wanted to make sure I understood for sure what it was. Thanks!

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

          You are welcome, and you are right that saving key strokes is no excuse.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • W William Winner

            I'm sure to some of you, this is pretty simple, and I think I know what it does, but I haven't been able to find the answer on the net. What exactly do the '?' and ':' mean in this bit of code?

            (dssubcat.Tables[0].Rows[0]["gallery_img"]) == System.DBNull.Value ? "" : dssubcat.Tables[0].Rows[0]["gallery_img"])

            Does that line say that if that item in the DataSet is equal to the DBNull value, then return "" and if it doesn't return the item?

            modified on Saturday, May 22, 2010 5:30 PM

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            Hi, You already have your answer so I won't expand on that but I thought this may be worth mentioning. I don't often use them because of readability issues they create. They are very useful however where it is not possible to use an if/else block such as when constructor chaining. For example, these are the constructors for an ImapClient class that I'm working on - the 3rd constructor needs to assign a port depending on the value of another parameter. I could put an if/else block inside the constructor itself but then I lose the benifit of only having code in one place that all other constructors call into. Using ?: solves this.

            public const int DefaultPort = 143;
            public const int DefaultPortSsl = 993;
            public static readonly ImapSecurity DefaultSecurity = ImapSecurity.Tls;

            private string hostname;
            private int port;
            private ImapSecurity security;

            public ImapClient(string hostname)
            : this(hostname, DefaultPort, DefaultSecurity)
            { }
            public ImapClient(string hostname, int port)
            : this(hostname, port, DefaultSecurity)
            { }
            public ImapClient(string hostname, ImapSecurity security)
            : this(hostname, security == ImapSecurity.Ssl ? DefaultPortSsl : DefaultPort, security)
            { }
            public ImapClient(string hostname, int port, ImapSecurity security)
            {
            this.hostname = hostname;
            this.port = port;
            this.security = security;
            }

            public enum ImapSecurity
            {
            None,
            Tls,
            Ssl,
            }

            Dave

            If this helped, please vote & accept answer!

            Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            1 Reply Last reply
            0
            • P Pete OHanlon

              This is known as the ternary operator, and you are right about what your assumption of the logic here is. Basically, it follows the pattern: (some boolean test) ? (part that gets executed if true) : (part that gets executed if false). It's very easy to get carried away with this operator and produce some code that looks like this:

              value < lowerBounds ? value > 0 ? Console.WriteLine("We have a problem") : Console.WriteLine("Value less than or equal to zero") : Console.WriteLine("The value is OK");

              If you must use it, use it with care; at some point you may be responsible for trying to figure out what you were smoking when you wrote the code.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              R Offline
              R Offline
              realJSOP
              wrote on last edited by
              #6

              I use this construct a lot, but never nest them - that's where you get into maintenance trouble down the road. I've seen code that had these nested as many as FIVE deep! MY EYES!

              .45 ACP - because shooting twice is just silly
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

              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