我不喜歡寬代碼,特別是當它迫使我滾動。所以寫了這個:縮短這個IF語句
If _item.SubItems(pd.perioddate).Text = "N/A" Or _item.SubItems(pd.perioddate).Text = String.Empty Then
dtpDeadlineforAP.Checked = False
End If
是否有一個體面的方式來減薄,使它更優雅?
我不喜歡寬代碼,特別是當它迫使我滾動。所以寫了這個:縮短這個IF語句
If _item.SubItems(pd.perioddate).Text = "N/A" Or _item.SubItems(pd.perioddate).Text = String.Empty Then
dtpDeadlineforAP.Checked = False
End If
是否有一個體面的方式來減薄,使它更優雅?
提取_item.SubItems(pd.perioddate).Text
爲局部變量,例如,
String text = _item.SubItems(pd.perioddate).Text
If text = "N/A" Or text = String.Empty Then
dtpDeadlineforAP.Checked = False
End If
或者,你可能想整個支票提取到一個單獨的方法:
If isNotFilled(_item.SubItems(pd.perioddate)) Then
dtpDeadlineforAP.Checked = False
End If
這將使代碼的可讀性更強,讓你重用檢查邏輯。
string obj = _item.SubItems(pd.perioddate).Text;
If obj = "N/A" Or obj = String.Empty Then
dtpDeadlineforAP.Checked = False
End If
ALSO
啓用Word經在Visual Studio停止不必滾動。
轉到
工具 - >選項 - >編輯 - 文本>所有語言 - >字經
不要忘記開啓 '顯示所有設置'
Dim date as String = _item.SubItems(pd.perioddate).Text
If date = "N/A" Or date = String.Empty Then
dtpDeadlineforAP.Checked = False
End If
With _item.SubItems(pd.perioddate)
If .Text = "N/A" Or .Text = String.Empty Then
dtpDeadlineforAP.Checked = False
End If
End With
提示關於WITH的優點/弊端的論據:)
如果有多個字段可以包含N/A,我會uggest以下方法:
Dim invalidValues As String() = {"N/A", String.Empty}
If invalidValues.Contains(_item.SubItems(pd.perioddate).Text) Then
dtpDeadlineforAP.Checked = False
End If
或者,如果它只是滾動,你可以使用VB續行符_
:
If _item.SubItems(pd.perioddate).Text = "N/A" _
Or _item.SubItems(pd.perioddate).Text = String.Empty Then
dtpDeadlineforAP.Checked = False
End If
BTW:在這裏,我建議OrElse
而不是Or
。
正如其他人所建議的那樣,您可以使用局部變量。您還可以使用line continuation字符_
另外縮短該行。
String period = _item.SubItems(pd.perioddate);
If period = "N/A" Or _
period = String.Empty Then
dtpDeadlineforAP.Checked = False
End If
我會suppgest一個助手類或ExtensionMethod:
If StringIsNullOrEmptyOrNA(stringval) Then
...
End If
If stringval.IsNullOrEmptyOrNa() Then
....
End If
Public Function StringIsNullOrEmptyOrNA(ByVal input as String) as Boolean
return String.IsNullOrEmpty(input) OrElse input.Equals("N/A")
End Function
<System.Runtime.CompilerServices.Extension()>
Public Function IsNullOrEmptyOrNa(ByVal input As String)
return String.IsNullOrEmpty(input) OrElse input.Equals("N/A")
End Sub
bear記住在VB.Net版本9.0(VS 2008)中引入了擴展方法。 OP沒有提及他正在使用的VB.Net/VS的版本。 – Jazza 2010-09-16 10:46:14
這就是爲什麼我首先在兩種方式中添加了示例;) – 2010-09-16 13:57:35
我們至少應該提到Select Case
Select Case _item.SubItems(pd.perioddate).Text
Case "N/A", ""
dtpDeadlineforAP.Checked = False
End Select
也可以考慮提取輔助函數
Function IsNotApplicable(ByVal s As String) As Boolean
Return (s = "N/A") Or (s = "")
End Function
我得到它下降到3行和63列。我用新的If operator取代了傳統的If construct。該代碼還將處理Text
作爲空引用的情況,並將使用OrElse
運算符進行短路。如果你願意宣佈一些擴展方法,你可以把它簡化成一條短線,但這與我的答案的精神相沖突。
Dim tx = _item.SubItems(pd.perioddate).Text
Dim dtp = dtpDeadlineforAP
dtp.Checked = If(tx = "N/A" OrElse tx = "", False, dtp.Checked)
我認爲我們仍然可以改進。 VB.Net將空引用字符串視爲等於「」,因此整個第一行可以被刪除。 – MarkJ 2010-09-16 19:49:31
@MarkJ:直到現在,我還沒有意識到這一點。感謝您指出了這一點! – 2010-09-16 19:59:18
我會重新命名文本變量意味着什麼,朦朧periodDate = _item.SubItems(pd.perioddate)。文本 – Iain 2010-09-16 11:14:30
我們還應該考慮選擇案例,而不是引入一個局部變量? 'Select Case _item.SubItems(pd.perioddate).Text Case「N/A」,「」dtpDeadlineforAP.Checked = False End Select' – MarkJ 2010-09-16 11:29:51
@Iain,同意了,我找不到一個好名字 - 正在考慮'findSomeMeaningfulNameForThis'一分鐘:-) – 2010-09-16 11:54:15