我有这个代码,我正在尝试删除行0 - 7都为零的行。做一个for循环,或者使用if-then会更好吗?:
tmpEditRow.BeginEdit()
Select Case a
Case 0
tmpEditRow("Qsold / QoH1") = QtySold & " / " & QtyOH
Case 1
tmpEditRow("Qsold / QoH3") = QtySold & " / " & QtyOH
Case 2
tmpEditRow("Qsold / QoH4") = QtySold & " / " & QtyOH
Case 3
tmpEditRow("Qsold / QoH7") = QtySold & " / " & QtyOH
Case 4
tmpEditRow("Qsold / QoH8") = QtySold & " / " & QtyOH
Case 5
tmpEditRow("Qsold / QoH10") = QtySold & " / " & QtyOH
Case 6
tmpEditRow("Qsold / QoH12") = QtySold & " / " & QtyOH
Case 7
tmpEditRow("Qsold / QoH14") = QtySold & " / " & QtyOH
End Select
tmpEditRow.EndEdit()
答案 0 :(得分:2)
为什么不使用变量进行计算,然后检查0值
更新:如果QtyOH是每个案例a
的当前值,那么它会更加简化:
If QtyOH <> 0 Then
tmpEditRow.BeginEdit()
Dim bucket as String = ""
Select Case a
Case 0
bucket = "1"
Case 1
bucket = "3"
Case 2
bucket = "4"
.
.
.
End Select
tmpEditRow("Qsold / QoH" & bucket) = QtySold & " / " & QtyOH
tmpEditRow.EndEdit()
End If
答案 1 :(得分:0)
使用Select Case
是可以的。但是,我倾向于组织代码,以便可以在可能的情况下将实际计算合并到单个块中(除非性能/优化/资源问题另有要求),以便进行读取和维护。
tmpEditRow.BeginEdit()
' Note: Assuming variables [QtySold] and [QtyOH] are numeric
' values, each set appropriately for whatever variable
' [a] is meant to indicate.
' [ColKey] is the column of interest within [tmpEditRow], as
' appropriate per [a].
Dim ColKey As String = Nothing
Select Case a
Case 0: ColKey = "Qsold / QoH1"
Case 1: ColKey = "Qsold / QoH3"
Case 2: ColKey = "Qsold / QoH4"
Case 3: ColKey = "Qsold / QoH7"
Case 4: ColKey = "Qsold / QoH8"
Case 5: ColKey = "Qsold / QoH10"
Case 6: ColKey = "Qsold / QoH12"
Case 7: ColKey = "Qsold / QoH14"
End Select
' The actual computation, right here, via [ColValue].
' [ColValue] is the text to place at [ColKey] of [tmpEditRow].
Dim ColValue As String = Nothing
If QtyOH = 0 Then
ColValue = ""
Else
ColValue = QtySold & " / " & QtyOH
End If
tmpEditRow(ColKey) = ColValue
tmpEditRow.EndEdit()
答案 2 :(得分:-1)
您可以将其浓缩为类似
的内容if ( a == 0 )
tmpEditRow("Qsold / QoH1") = QtySold & " / " & QtyOH
else if ( a == 1 )
tmpEditRow("Qsold / QoH3") = QtySold & " / " & QtyOH
else if ( a == 3 )
tmpEditRow("Qsold / QoH7") = QtySold & " / " & QtyOH
else if (a == "2|[5-7]") //regex comparison
tmpEditRow("Qsold / QoH" + (a*2) ) = QtySold & " / " & QtyOH
else
tmpEditRow.EndEdit()
修改强>
我刚注意到“QoH_”部分不是通过乘以2来创建的。在任何情况下,您仍然可以使用if / else块执行此操作。