Ternary operator; What is wrong with this syntax?
I'm trying to create a MailMessage, and I'm getting the following error...
Cannot implicitly convert type 'string' to 'bool'
This is my init statement:
MailMessage msg = new MailMessage("DoNotReply@optoma.com",
myTbl.Rows[i]["Requester"].ToString().Trim(),
subject,
"Dear " + myTbl.Rows[i]["Ship_Attention"].ToString() + ",<br/><br/>" +
body + "<br/>Your ISO ID is " + myTbl.Rows[i]["ISO_ID"].ToString() +
(Convert.ToInt32(myTbl.Rows[i]["EmailType"]) == 1) ?
("<br/>Tracking Number: " + myTbl.Rows[i]["Tracking_No"].ToString()) :
("") + "<br/><br/>Please examine the loaned items for this transaction:<br/><br/>" +
sw.ToString());
I'm trying to add to the string at runtime based on a boolean expression. 开发者_开发技巧Why can't I do this? Am I not doing it right?
string + (true | false) ? "somestring" : "anotherstring" + string
the ? : operator has very low precedence. Put it in parenthesis and I think you'll resolve your issue.
((true|false)?"somestring":"anotherstring")
When you have string + (bool)?"somestring":"anotherstring" + string
the +
gets evaluated before the ?
, so you need parentheses:
string + ((bool)?"somestring":"anotherstring") + string
just cleaning it up a wee bit.... and you won't run into operator precedence problems so much
void SendMessage(DataRow row, string subject, string body, string sw)
{
var to = row["Requester"].ToString().Trim();
var isoId = row["ISO_ID"].ToString();
var attention = row["Ship_Attention"].ToString();
var emailType = Convert.ToInt32(row["EmailType"]);
var message = (emailType == 1) ? ("<br/>Tracking Number: " + row["Tracking_No"]) : ("");
MailMessage msg = new MailMessage("DoNotReply@optoma.com",
to,
subject,
string.Format("Dear {0},<br/><br/>{1}<br/>Your ISO ID is {2}{3}<br/><br/>Please examine the loaned items for this transaction:<br/><br/>{4}",
attention, body, isoId, message, sw));
}
The precedence isn't what you expect -- +
is being evaluated first. Your code should be in the form:
string + (true|false ? "somestring" : "anotherstring") + string
For your specific example:
MailMessage msg = new MailMessage("DoNotReply@optoma.com", myTbl.Rows[i]["Requester"].ToString().Trim(),
subject, "Dear " + myTbl.Rows[i]["Ship_Attention"].ToString() + ",<br/><br/>" +
body + "<br/>Your ISO ID is " + myTbl.Rows[i]["ISO_ID"].ToString() + (Convert.ToInt32(myTbl.Rows[i]["EmailType"]) == 1 ? ("<br/>Tracking Number: " + myTbl.Rows[i]["Tracking_No"].ToString()) : ("")) + "<br/><br/>Please examine the loaned items for this transaction:<br/><br/>" +
sw.ToString());
Note that this is a very long expression and should probably be broken down into several statements to make it more readable and maintainable.
The additive oprator (+) has higher priority than conditional (?:) as indicated here: http://msdn.microsoft.com/en-us/library/aa691323%28v=vs.71%29.aspx.
Therefore you need to put parenthesis around the whole condition:
string + ((true|false)?"somestring":"anotherstring") + string
I suggest you to divide your code into more lines, introduce some temporary variables and use string.format() to make it look clener. Finding errors in clean code is much easier.
精彩评论