How would I convert the following code to use the ?: operator.. Is it Possible?
tbtotalamount.Text = string.Format("{0:n2}", dtl.Compute("sum(NetPay)", ""));
if (tbtotalamount.Text.Length == 0)
{
tbtotalamount.Text = "0";
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The quoted code wouldn’t benefit from using the
? :operator, which is called the conditional operator (sometimes called “the ternary operator” although technically, it’s only a ternary operator — e.g., an operator that has three operands).Typically the conditional operator is handy for when you have a variable and want to assign one of two values to it on the basis of a condition. So code in this form:
can be rewritten
In your case, though, you don’t know that
tbtotalamount.Textis blank until after you’ve done thestring.Format, so you’re better off leaving it with theif.