Function result_ss() As String
If RadioButton_ss1.Checked = True Then result_ss = "1"
If RadioButton_ss2.Checked = True Then result_ss = "2"
End Function
編譯器報錯:Function doesn't return a value on all code paths。當兩個 RadioButton 均未被選中時,函數無返回值。
Function result_ss() As String
result_ss = "" ' 或 "0",視業務默認值而定
If RadioButton_ss1.Checked Then result_ss = "1"
If RadioButton_ss2.Checked Then result_ss = "2"
' 可選:ElseIf 鏈確保互斥
End Function
' Select Case 更清晰表達互斥關係
Function result_ss() As String
Select Case True
Case RadioButton_ss1.Checked : Return "1"
Case RadioButton_ss2.Checked : Return "2"
Case Else : Return ""
End Select
End Function
' 單行 Lambda 風格 (VB 14+)
Function result_ss() As String = If(RadioButton_ss1.Checked, "1", If(RadioButton_ss2.Checked, "2", ""))
Return)。ElseIf 或 Select Case 表達語義更準確。Boolean 或拋出異常,而非魔術字串。一行初始化,徹底解決「不在所有路徑返回值」編譯錯誤。