2014-09-26 58 views
-3

計劃方法上Button_Event錯誤在VB程序

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim obj As New clsBook 
     obj.show(5) 
     obj.show(5, "JAVA") 
     obj.show(5, "JAVA", "XYZ") 
    End Sub 

錯誤超載 類書

Public Class clsBook 
    Public Function show(ByVal bn As Integer) 
     MsgBox(bn) 
    End Function 
    Public Function show(ByVal bn As Integer, ByVal bna As String) 
     MsgBox(bn, bna) 
    End Function 
    Public Function show(ByVal bn As Integer, ByVal bna As String, ByVal bp As String) 
     MsgBox(bn, bna, bp) 
    End Function 
End Class 

=

Conversion from string "JAVA" to type 'Integer' is not valid. 

任何人都可以請解釋如何去解決這個?

+5

閱讀MSGBOX :) – 2014-09-26 12:52:51

+1

文檔這將是很難找到的命名不佳的更完整的例子比這個代碼... – David 2014-09-26 12:53:20

+1

您應該嘗試通過代碼進行調試/步進查看。但非常多,字符串'JAVA'不是int – 2014-09-26 12:53:53

回答

1

是否要將show()中的所有參數連接到單個msgbox中?如果是

,它應該是這樣的:

Public Class clsBook 
    Public Function show(ByVal bn As Integer) 
     MsgBox(bn) 
    End Function 
    Public Function show(ByVal bn As Integer, ByVal bna As String) 
     MsgBox(bn & " " & bna) 
    End Function 
    Public Function show(ByVal bn As Integer, ByVal bna As String, ByVal bp As String) 
     MsgBox(bn & " " & bna & " " & bp) 
    End Function 
End Class 

,但如果你想在一個單獨的msgbox每個參數顯示:

Public Class clsBook 
     Public Function show(ByVal bn As Integer) 
      MsgBox(bn) 
     End Function 
     Public Function show(ByVal bn As Integer, ByVal bna As String) 
      MsgBox(bn) 
      MsgBox(bna) 
     End Function 
     Public Function show(ByVal bn As Integer, ByVal bna As String, ByVal bp As String) 
      MsgBox(bn) 
      MsgBox(bna) 
      MsgBox(bp) 
     End Function 
End Class 
0

按照MicroSoft's解釋的第二個參數的MsgBox()功能是按鈕: 它是可選的,數字表達式是值的總和,指定要顯示的按鈕的數量和類型,圖標樣式使用,默認按鈕的標識以及消息框的模態。

語法MSGBOX()是:

Public Function MsgBox(_ 
    ByVal Prompt As Object, _ 
    Optional ByVal Buttons As MsgBoxStyle = MsgBoxStyle.OKOnly, _ 
    Optional ByVal Title As Object = Nothing _ 
) As MsgBoxResult 

因此當調用函數作爲MsgBox(bn, bna)然後第二個參數必須是一個整數值,但你給"JAVA"其給出錯誤。

要充分填滿你的任務,你需要改變它爲:

MsgBox(bn & "," & bna)