在VB选择案例中多次返回

时间:2017-01-23 23:14:27

标签: c# vb.net

我正在编写从一些旧版VB代码转换而来的C#代码。其中一个函数的VB代码是:

Private Function function1() As Double

        Dim variable2 As Double = 0

        If variable1 = 6 Then
            Select Case variable3
                Case Is <= 1500 : Return Constant1
                Case Is <= 2000 : Return Constant2
                Case Is <= 2500 : Return Constant3
                Case Is <= 3000 : Return Constant4
                Case Else : Return Constant5
            End Select
            variable2 = variable2 +3
        End If
        Return variable2
End Function

变量1,变量3和所有常量都是类级字段。 在If语句中处理后,返回值似乎来自变量2。

有人可以建议为什么select case语句中有多个return子句?我的猜测是它为变量3分配了常量值以供将来进程使用,有人同意我的意见吗?

2 个答案:

答案 0 :(得分:2)

Select Case就是VB.NET等同于C#的开关... case

https://msdn.microsoft.com/en-us/library/cy37t14y.aspx

您在案例中返回的任何值都将从function1本身返回。

如果在所有情况下都有回报,则声明:

variable2 = variable2 +3

......无法接触......

Return variable2

将始终返回0.

答案 1 :(得分:0)

我不认为有任何多个返回变量。您的代码可能存在错误分配

  

variable2 = variable2 +3 //这一行永远不会执行。

要执行此行,您需要将常量值分配给variable2,而不是从每种情况返回。

最后,如果variable1不等于6,则此函数将始终返回0.

相关问题