如何在事件子例程(vb.net)之间传递变量

时间:2017-12-01 13:42:44

标签: vb.net vb.net-2010

所以我在Vb.net上制作游戏。我在一个子程序中使用2D数组,在按下按钮时执行它。所以我在按下按键的子程序中定义了变量。

Private Sub GameScreen_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

    Dim r = 5 - 1
    Dim GameState(r, r)

我想使用" r"和#34; GameState"另一个事件子程序中的变量

Private Sub GameScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For no = 0 To r
        For no2 = 0 To r
            If GameState(no, no2) = 0 Then
                Counter += 1
            End If
        Next
    Next

但是,当我尝试在子程序中添加另一个参数时,它就不起作用了。

Private Sub GameScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs, ByRef GameState(,) As Integer) Handles MyBase.Load
    For no = 0 To r
        For no2 = 0 To r
            If GameState(no, no2) = 0 Then
                Counter += 1
            End If
        Next
    Next

1 个答案:

答案 0 :(得分:1)

您无法向事件处理程序添加参数。您可以使用类级变量。只需将您的r变量类级别与其他方法共享即可。

Public Class Test
    Private r As Integer

    Public Sub Method()
        ' You can use r here
    End Sub

    Public Sub Method2()
        ' You can also use r here
    End Sub
End Class