我在mybase.load中有一个类的数组,我想在很多其他子中使用它。 mybase.load和按钮单击都是我使用过的公共子。它是一个初学者的问题。
干杯
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Contracts() As Contract
ReDim Contracts(CC)
For c = 0 To CC
Contracts(c) = New Contract
Next
我可以应用模块范围吗?
答案 0 :(得分:0)
表单Load和Button事件不需要是Public,也可能不应该是。但它们与变量Scope
没有任何关系。
Public Class frmMyForm
' declared outside a SUB/Function, these have
' module level scope
Private myFoo As Integer
Private myBar As String
Private Sub frmMyForm_Load(...
' some code to do some stuff
myFoo = 1
myBar = "ziggy"
End Sub
Private Sub btnToDoSomething(....
If myFoo <> 1 Then Exit Sub
' other code
End Sub
End Class
这些变量也可以在构造函数(Sub New
)中初始化,如果表单在实例化时需要具有这些值,或者如果表单不存在则不存在。