2011-03-19 53 views
1

考慮:爲什麼此函數不會在所有代碼路徑上返回一個值?

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 
End Function 

爲什麼ViewPropertyCast不能在所有代碼路徑返回一個值?

回答

3

您沒有默認值。如果它不符合任何情況怎麼辦?添加一個返回錯誤的默認值,或者在選擇後添加一個。你可能確定你永遠不會傳遞任何東西,但編譯器不是。

5

因爲如果type沒有列出的那些東西,它只是下降到沒有返回任何東西的底部。

嘗試類似:

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 
1

你沒有默認情況。如果我傳遞「foo」,該函數將無法返回值。

相關問題