使用ByRef和ByVal

时间:2013-03-26 11:27:41

标签: vb.net byref byval

我对编程很新,我被告知我应该使用ByRef和ByVal传递内容,但是当我这样做时,我收到一个错误说:

Error   3   Method 'Private Sub Activate_Click(ByRef intIDToChange As Integer, sender As Object, e As System.EventArgs)' 
cannot handle event 'Public Event Click(sender As Object, e As System.EventArgs)' because they do not have a compatible signature.  
F:\Dropbox\Gooby Backup\School Work\Computing\Unit 4\Room Booking Client\WindowsApplication1\ActivateDeactivate\Activate Deactivate.vb  32  129 WindowsApplication1

我开始使用:

Private Sub Activate_Click(ByRef intIDToChange As Integer, sender As System.Object, e As System.EventArgs) Handles Activate.Click

3 个答案:

答案 0 :(得分:2)

您无法使用您的方法处理Activate.Click,因为您有额外的Integer参数与事件签名不兼容。

活动签名

Click(sender As Object, e As System.EventArgs)

您的方法

Activate_Click(ByRef intIDToChange As Integer, sender As System.Object, e As System.EventArgs)

答案 1 :(得分:0)

您必须将方法定义为

Private Sub Activate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Activate.Click

因为按钮单击事件只有两个参数。

如果要使用变量intIDToChange,请将其作为类级变量(如

)提供

private intIDToChange as Integer并在Activate_Click更新。

答案 2 :(得分:0)

这听起来就像你在那里声明的事件处理程序(Activate_Click)与ActivateClick的事件定义不匹配。

尝试更改为:

Private Sub Activate_Click(ByVal intIDToChange As Integer, ....) Handles Activate.Click

ByVal通过参数发送变量的值,而ByRef发送变量(意味着子程序中的更改会影响调用例程变量)。

这里有更详细的答案: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/07b9d3b9-5658-49ed-9218-005564e8209e/