0

我有这段代码,我正在尝试删除 case 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()
4

3 回答 3

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
于 2012-12-27T20:39:11.357 回答
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()
于 2012-12-27T21:27:02.470 回答
-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 块来执行此操作。

于 2012-12-27T20:36:27.470 回答