Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this query from temp table 'DTLocal' data shown textbox. Here I cann't understand why ? marks is used. please help me

C#
txtTruckReceiptDate.Text = (DTLocal.Rows[0]["TruckReceiptDate"].ToString() == "" ? "" : DateTime.Parse(DTLocal.Rows[0]["TruckReceiptDate"].ToString()).ToString("dd-MMM-yyyy"));

txtDocHODate.Text = (DTLocal.Rows[0]["DOCHODate"].ToString() == "" ? "" : DateTime.Parse(DTLocal.Rows[0]["DOCHODate"].ToString()).ToString("dd-MMM-yyyy"));
Posted
Updated 8-Apr-13 21:45pm
v2

Its basically a ternary operator. The question mark combined with the colon sign.
Works like this:

C#
string name = ABooleanExpression ? "Jack" : "Jill";


If the boolean exp is true then the variable name will be assigned as Jack, if not then the variable name will be Jill.

Refer this understanding in your code and you will get it.
Hope it helps.

-ANurag
 
Share this answer
 
Comments
Maciej Los 9-Apr-13 3:57am    
+5!
Anurag Sinha V 9-Apr-13 4:10am    
thank you... :)
CPallini 9-Apr-13 3:59am    
Right, my 5. I completely misunderstood the code :-O
Anurag Sinha V 9-Apr-13 4:11am    
thank you.. :)
It is a Ternary Operator[^].

Quote:
condition ? first_expression : second_expression;
For first line of code...
condition - DTLocal.Rows[0]["TruckReceiptDate"].ToString() == ""
first_expression - ""
second_expression - DateTime.Parse(DTLocal.Rows[0]["TruckReceiptDate"].ToString()).ToString("dd-MMM-yyyy"))

If condition is satisfied the first_expression will be executed, otherwise second_expression will be executed.

So, for first line...
C#
txtTruckReceiptDate.Text = (DTLocal.Rows[0]["TruckReceiptDate"].ToString() == "" ? "" : DateTime.Parse(DTLocal.Rows[0]["TruckReceiptDate"].ToString()).ToString("dd-MMM-yyyy"));

Here it checks the value of DTLocal.Rows[0]["TruckReceiptDate"].ToString().
And
-> if the value is "", it assigns "" to txtTruckReceiptDate.Text,
-> otherwise it assigns DateTime.Parse(DTLocal.Rows[0]["TruckReceiptDate"].ToString()).ToString("dd-MMM-yyyy")) to txtTruckReceiptDate.Text
 
Share this answer
 
Comments
Anurag Sinha V 9-Apr-13 4:10am    
apt explanation....+5 from me..
Thanks @Anurag...
Anurag Sinha V 9-Apr-13 4:16am    
np..:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900