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