vb代码解释??!

来源:百度知道 编辑:UC知道 时间:2024/05/13 08:49:02
2段代码

通过下面程序观察全局变量和局部变量的作用域
Option Explicit
Public X1 As Integer
Private Sub Form_Load()
X1 = 2004
End Sub
Private Sub Command1_Click()
X1 = X1 + 1
Test
End Sub
Private Sub Test()
Dim X1 As String
X1 = "年毕业"
Debug.Print "她于:"; Form1.X1; X1
End Sub

通过下面程序观察Static变量的生存期
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
Static X1: Dim X2
If KeyAscii = 13 Then
X1 = X1 + 1: X2 = X2 + 1
Debug.Print X1, X2
If Text1.Text = "11" Then
MsgBox "通过"
Text1.Enabled = False
Else
If X1 = 3 Then
MsgBox "输入无效口令已达三次,退出程序"
Else
MsgBox "无效口令,请再输一次"
Text1.Text = ""
End If
End If
End If
End Sub

谢谢 100分 略表心意

我来解释
Option Explicit '强制变量声明
Public X1 As Integer '声明公有的模块级变量
Private Sub Form_Load() '窗体装载事件
X1 = 2004 '变量赋值
End Sub
Private Sub Command1_Click() 'Command1单击事件
X1 = X1 + 1 'X1值加1
Test '调用过程Test
End Sub
Private Sub Test() '声明一个过程
Dim X1 As String '声明变量X1为字串变量
X1 = "年毕业" '赋值
Debug.Print "她于:'"; Form1.X1; X1 '在调试窗口输出
End Sub

'通过下面程序观察Static变量的生存期
'Option Explicit'这句多余
Private Sub Text1_KeyPress(KeyAscii As Integer) 'text1的按键事件
Static X1: Dim X2 '声明局部变量
If KeyAscii = 13 Then '如果按下"Enter"键
X1 = X1 + 1: X2 = X2 + 1 '这两个值都加1
Debug.Print X1, X2 '调试窗口输出这两个变量值
If Text1.Text = "11" Then '如果文本框中输入11
MsgBox "通过" '弹出消息框
Text1.Enabled = False 'text1不可用
Else '如果不是输入11
If X1 = 3 Then '嵌套
MsgBox "输入无效口令已达三次,退