开发者

Why does this function not return a value on all code paths?

开发者 https://www.devze.com 2023-02-17 16:54 出处:网络
Consider: Private Function ViewPropertyCast(ByVal type As String) As String Select Case type Case \"smallint\"

Consider:

Private Function ViewPropertyCast(ByVal type As String) As String
  Select Case type
    Case "smallint"
      Return "Sho开发者_StackOverflow中文版rt"
    Case "nvarchar"
      Return "String"
    Case "int"
      Return "Integer"
    Case "float"
      Return "Double"
    Case "datetime"
      Return "Date"
    Case "bigint"
      Return "Long"
    Case "ntext"
      Return "String"
    Case "bit"
      Return "Boolean"
    Case "uniqueidentifier"
      Return "Guid"
  End Select
End Function

Why does ViewPropertyCast not return a value on all code paths?


Because if type is none of those things listed, it simply drops to the bottom where nothing is returned.

Try something like:

Private Function ViewPropertyCast(ByVal type As String) As String
    Select Case type
        Case "smallint"
            Return "Short"
        Case "nvarchar"
            Return "String"
        Case "int"
            Return "Integer"
        Case "float"
            Return "Double"
        Case "datetime"
            Return "Date"
        Case "bigint"
            Return "Long"
        Case "ntext"
            Return "String"
        Case "bit"
            Return "Boolean"
        Case "uniqueidentifier"
            Return "Guid"
    End Select
    Return "NoIdea"               ' <-- Added this bit '
End Function


You have no default. What if it matches none of the cases? Add a default that returns an error, or put one after the select. You may be sure you'll never be passed anything else, but the compiler isn't.


You have no default case. If I pass "foo" the function can not return a value.

0

精彩评论

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