开发者

shorthand syntax for a radiobuttonlist SelectedItem.Text only if one is selected?

Sorry, difficult to give the question a proper title?

I'm basically appending a lot of text together to form the email body in my application from several radiobuttonlists in my web form. So my code looks like this:

StringBuilder body = new StringBuilder();
body.Append("<strong>For question1: </strong>  " + ((rblQuestion1.SelectedIndex > -1) ?  rblQuestion1.SelectedItem.Text : "") + "<br /><br />");

So my question is do I have to use the following syntax:

((rblQuestion1.SelectedIndex > -1) ?  rblQuestion1.SelectedItem.Text : "")

Where what I really want is:

(rblQuestion1.SelectedIndex > -1) ?  rblQuestion1.SelectedItem.Text 

I don't need the empty string alternative if false, if you understand my point.

It's just me being a bit pedantic with my code real开发者_开发知识库ly. Thanks in advance.


The only alternative I see when you per se need to check for

rblQuestion1.SelectedIndex > -1

is the following:

if (rblQuestion1.SelectedIndex > -1)
  someStringVar = rblQuestion1.SelectedItem.Text;

There is no other solution because conditional statements need to be formatted in the following pseudo-code:

toBeAssigned = boolean expression ? assigner if true : assigner if false;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜