开发者

No return keyword for this Function in Visual Basic .NET

开发者 https://www.devze.com 2023-03-26 19:11 出处:网络
I\'ve been doing some code review and this code seemed weird to me since it doesn\'t have any return statement:

I've been doing some code review and this code seemed weird to me since it doesn't have any return statement:

Protected Function AddZero(ByVal vsInput As String) As String
    If Len(vsInput) = 1 Then
        AddZero = "0" & vsInput
   开发者_运维技巧 Else
        AddZero = vsInput
    End If
End Function


Visual Basic treats the function name as the return value and does not return until the end of the function. In the code, you can see that AddZero (the function name) is set to one of two values depending on the if condition. That is how you can determine what is returned.


In VB, you have an implicit return at the end of the function.

The function name gets assigned the return value, like this:

Protected Function AddZero(ByVal vsInput As String) As String
     AddZero = "0"  ' The return value is "0"
End Function

You can exit a function (return) like this:

Protected Function AddZero(ByVal vsInput As String) As String
     If vsInput = "0" Then
        AddZero = vsInput;
        Exit Function
     End If

     AddZero = "0"  ' The return value is "0"
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消