重载过程的优势体现在调用的灵活性。 调用代码可以获取它需要传递给过程的信息,然后调用单个过程名称,无论它传递的参数是什么。
调用定义了多个版本的过程
在调用代码中,确定要传递给过程的数据。
以正常方式编写过程调用,在参数列表中显示数据。 请确保参数与为该过程定义的某个版本中的参数列表匹配。
无需确定要调用的过程的版本。 Visual Basic 将控制权传递给与参数列表匹配的版本。
下面的示例调用
post
中声明的 过程。 它获取客户标识,确定它是一个String
还是一个Integer
,然后在任一情况下调用相同的过程。Imports MSVB = Microsoft.VisualBasic
Dim customer As String Dim accountNum As Integer Dim amount As Single customer = MSVB.Interaction.InputBox("Enter customer name or number") amount = MSVB.Interaction.InputBox("Enter transaction amount") Try accountNum = CInt(customer) Call post(accountNum, amount) Catch Call post(customer, amount) End Try