ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Public vs. Local Variables

Public vs. Local Variables

原创 Linux操作系统 作者:ljm0211 时间:2012-07-02 10:53:05 0 删除 编辑

You can also have a variable with the same name at a different scope. For example, you could have a public variable named Temp and then, within a procedure, declare a local variable named Temp. References to the name Temp within the procedure would access the local variable; references to Temp outside the procedure would access the public variable. The module-level variable can be accessed from within the procedure by qualifying the variable with the module name.

Public Temp As Integer
Sub Test()
Dim Temp As Integer
Temp = 2 ' Temp has a value of 2.
MsgBox Form1.Temp ' Form1.Temp has a value of 1.
End Sub

Private Sub Form_Load()
Temp = 1 ' Set Form1.Temp to 1.
End Sub
Private Sub Command1_Click()
Test
End Sub

In general, when variables have the same name but different scope, the more local variable always shadows (that is, it is accessed in preference to) less local variables. So if you also had a procedure-level variable named Temp, it would shadow the public variable Temp within that module.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11411056/viewspace-734288/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-05-14

  • 博文量
    272
  • 访问量
    436870