2009-11-25 80 views
1

對不起,很難給這個問題一個正確的標題?只有選擇了一個選項時,選擇了一個單選按鈕SelectedItem.Text的簡寫語法?

我基本上是在我的web表單的幾個單選按鈕列表中添加大量文本,以形成我的應用程序中的電子郵件正文。所以,我的代碼如下所示:

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

所以我的問題是我必須使用以下語法:

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

當我真正想要的是:

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

我不如果你瞭解我的觀點,如果錯誤的話,不需要空白字符串。

這只是我真的有點迂腐與我的代碼。提前致謝。

回答

0

我看的時候,你本身需要檢查

rblQuestion1.SelectedIndex > -1 

,唯一的選擇是:

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

有沒有其他的解決方案,因爲條件語句需要在以下進行格式化僞代碼:

toBeAssigned = boolean expression ? assigner if true : assigner if false; 
相關問題