重构select case语句

时间:2013-11-22 13:08:40

标签: vb.net

我有一个过程,在需要时从后端表中获取值。我一直努力确保只有当存储过程检索该值所需的参数确实产生一个值时,才会引用该表,这导致我进入以下select case语句。

我已经多次研究了几次尝试重构它以使其更高效(或至少不那么繁琐)但未能提出任何建议。所以我想我会问是否有人能够发现一种更有效的方法,更重要的是要指出为什么它更有效率。

为了更容易理解,这是为了确定确定在港口降落的鱼的活重可能由其着陆方式确定的转换因子。因此,例如,如果您只是放置鱼片,那么相当数量的整条鱼将会浪费掉,但需要考虑废物。 PC是演示代码(即" FIL"是圆角的)。并非所有物种(由SC表示)都具有所有表示代码的转换因子,因此需要将其缩小,以便仅在存在可能提供有效转换因子的情况下调用存储过程(GetConversionFactor)。 p>

非常感谢您提出的任何建议。

Private Sub GetConversionFactor()
    'not all presentation codes have a conversion factor
    ' and not all codes need to be checked
    Select Case PC
        Case "WHL"
            ConversionFactor = CDec(0.0)
        Case "GUT"
            ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
        Case "GUH"
            Select Case SC
                Case "ANF", "BLL", "COD", "DAB", "HAD", "HKE", "HER", "LIN", "PLE", "POL", "POK", "WHG", "BLI", "GFB", "RNG"
                    ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
                Case Else
                    ConversionFactor = CDec(0.0)
            End Select
        Case "FIL"
            Select Case SC
                Case "BSS", "COD", "COE", "SOL", "FLE", "HAL", "HAD", "HKE", "LEM", "LIN", "LEZ", "MUL", "PLE", "POL", "MUR", "POK", "DGS", "TUR", "WHG", "WIT", "BLI", "GFB", "RNG", "SBR"
                    ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
                Case Else
                    ConversionFactor = CDec(0.0)
            End Select

        Case "TAL"
            Select Case SC
                Case "ANF", "NEP"
                    ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
                Case Else
                    ConversionFactor = CDec(0.0)
            End Select

        Case "HEA"
            Select Case SC
                Case "COD", "BSF"
                    ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
                Case Else
                    ConversionFactor = CDec(0.0)
            End Select

        Case "GUS"
            Select Case SC
                Case "FLE", "DGS"
                    ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
                Case Else
                    ConversionFactor = CDec(0.0)
            End Select

        Case "SGT"
            Select Case SC
                Case "LIN"
                    ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
                Case Else
                    ConversionFactor = CDec(0.0)
            End Select
        Case "WNG"
            Select Case SC
                Case "RJB", "RJO", "JAD", "RJA", "RJH", "RJM", "RJR", "RJC", "RJE", "RJN", "RJI", "RJU", "RJF", "TTR", "TTO", "JDP"
                    ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
                Case Else
                    ConversionFactor = CDec(0.0)
            End Select
        Case "GHT"
            Select Case SC
                Case "RNG"
                    ConversionFactor = CDec(te.GetConversionFactor(SC, PC).FirstOrDefault)
                Case Else
                    ConversionFactor = CDec(0.0)
            End Select
        Case Else
            ConversionFactor = CDec(0.0)
    End Select

End Sub

1 个答案:

答案 0 :(得分:0)

你可以预先评估PC:

 IF "|WHL|GUT|FIL|etc|".Contains("|" & PC) & "|" THEN