我不熟悉VB.NET,为了尝试理解遗留代码(格式粗糙),我保持更好,我把它输入缩进机here。
但这是其中的一部分:
currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS = New ADODB.Recordset
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
Category = "New Business"
If Not adoRS.EOF Then
IsNewBusiness = adoRS.Fields.Item(0).Value <> 0
if Not IsNewBusiness
Category = "Existing Business"
End If
Response.Write("<!-- IsNewBusiness after NOT adoRS.EOF check = " & CStr(IsNewBusiness) & " -->")
End If
adoRS.Close()
If Request.Form.Item("Action") = "Save" Then
Response.Write("<!-- Made it into the Action =Save block -->")
Unit = Request.Form.Item("Unit")
. . .
这是对的吗?在我看来它应该更像这样:
currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS = New ADODB.Recordset
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
Category = "New Business"
If Not adoRS.EOF Then
IsNewBusiness = adoRS.Fields.Item(0).Value <> 0
if Not IsNewBusiness
Category = "Existing Business"
End If
Response.Write("<!-- IsNewBusiness after NOT adoRS.EOF check = " & CStr(IsNewBusiness) & " -->")
End If
adoRS.Close()
If Request.Form.Item("Action") = "Save" Then
Response.Write("<!-- Made it into the Action =Save block -->")
Unit = Request.Form.Item("Unit")
. . .
我知道它只是格式化,并且它不会导致代码以任何不同的方式工作,但它可以帮助我很多正确格式化(特别是使用“if”和“endif”等等)排队)。
任何时候有“如果”,必须有相应的“结束如果”对吗?
假设是这样,这个简洁的代码更令人困惑:
If Not adoRS.EOF Then
CustomerChk = adoRS.Fields.Item(0).Value
adoRS.Close()
If CustomerChk <> CustNo Then
......不应该是:
If Not adoRS.EOF Then
CustomerChk = adoRS.Fields.Item(0).Value
adoRS.Close()
If CustomerChk <> CustNo Then
?否则,它会使它看起来像第一个if块结束而没有任何明确的说法。
答案 0 :(得分:1)
关于你的VB.NET问题......
1)这[代码缩进更新]是否正确?
对我来说看起来不错。我可能会在if代码块之前和之后插入空白行,但这只是我的个人偏好而不是VB.NET要求。
2)任何时候有“如果”,必须有相应的“结束如果”权利吗?
通常这是约定,但这不是VB.NET的要求。 VB.NET允许单行“If”语句,如此MSDN page上引用的以下行。
' If A > 10, execute the three colon-separated statements in the order
' that they appear
If A > 10 Then A = A + 1 : B = B + A : C = C + B