Vb程序出错

时间:2014-09-26 12:49:29

标签: vb.net

方法重载程序  课本

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

on Button_Event

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

错误=

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

有人可以解释一下如何解决这个问题吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

根据MicroSoft's解释MsgBox()函数中的第二个参数是按钮: 它是可选的,Numeric表达式是指定要显示的按钮的数量和类型的值的总和,要使用的图标样式,默认按钮的标识,以及消息框的形式。

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)