diddom 发表于 2012-6-6 07:49:15

Global scope


'demonstrates the use of global variables and constants in a WSH scri

Option Explicit

'any variable or constant declared here will be available to
'all scripts in the document
Dim lngMyVar
Const my_Const=5

GetUserInput()
'use lngMyVar in unrelated procedures just to check whether it's global
MySecondProcedure
IncrementValue
MySecondProcedure
MultiplyConstant
MySecondProcedure

Sub GetUserInput()

   'lngMyVar does not need to be declared here - it's global
   lngMyVar = InputBox("Enter a Number: ", "Script-Level", 0)


End Sub

Sub MySecondProcedure()
   'display the value of lngMyVar
   MsgBox "lngMyVar: " & lngMyVar
End Sub

Sub IncrementValue

   'let's add the value of the global constant to lngMyVar
   lngMyVar = lngMyVar + my_Const

End Sub

Sub MultiplyConstant

   lngMyVar = lngMyVar + (my_Const * 2)

End Sub
页: [1]
查看完整版本: Global scope