SCOPE of a variable

The scope of a variable refers to its accessibility among procedures. When a variable is declared within an event, it is only visible within the code for that event. Variables of this nature are known as "local in scope". No other procedure can refer to that variable or change its value..

Variables and constants declared in the General Declerations section of a form module are accessible to every procedure in the module. When a variable is declared in this manner, it is considered "global in scope".

--------------------------------------------------------------------------------------------------------

Local variables should be declared whenever possible because their use does not interfere with the operation of other procedures. A global variable should be declared when more than one procedure needs access to it.

-------------------------------------------------------------------------------------------------------

DRILL 1 If the following three Click events are coded, what would the output be if the command buttons were clicked in the following order: cmdInitialize, cmdAdd, and cmdOutput?

Private Sub cmdInitialize_Click( )

Dim Value as integer

Value = 10

End Sub

 

Private Sub cmdAdd_Click( )

Dim Value as integer

Value = Value + 10

End Sub

 

Private Sub cmdOutput_Click( )

Dim Value as integer

MsgBox Str(Value)

End Sub

----------------------------------------------------------------------------------------------------------------------

DRILL 2 If the following three Click events are coded, and the variable Value is declared in the General Declerations section, what would be the output if the command buttons were clicked in the following order: cmdInitialize, cmdAdd, cmdOutput?

Dim Value as Integer

Private Sub cmdInitialize_Click( )

Value = 10

End Sub

 

Private Sub cmdAdd_Click( )

Value = Value + 10

End Sub

 

Private Sub cmdOutput_Click( )

Msg Str(Value)

End Sub

 

Back to Main Page