VARIABLES
Variables are among the most fundamental building blocks of a program. A variable is a program object that stores a value. The value can be a number, letter, string, date, structure containing other values, or an object representing both data and related actions.
When a variable contains a value, the program can manipulate it. It can perform arithmetic operations on numbers, string operations on strings (concatenation, calculating substrings,
f nding a target within a string), date operations (f nd the difference between two dates, add a
time period to a date), and so forth.
Four factors determine a variable’s exact behavior:
➤ Data type determines the kind of data it can hold (integer, character, string, and so forth).
➤ Scope def nes the code that can access the variable. For example, if you declare a variable
inside a For loop, only other code inside the For loop can use the variable. If you declare a
variable at the top of a subroutine, all the code in the subroutine can use the variable.
➤ Accessibility determines what code in other modules can access the variable. If you declare
a variable at the module level (outside of any subroutine in the module) and you use the
Private keyword, then only the code in the module can use the variable. If you use the Public
keyword, then code in other modules can use the variable as well.
➤ Lifetime determines how long the variable’s value is valid. A variable inside a subroutine
that is declared with a normal Dim statement is created when the subroutine begins and is
destroyed when it exits. If the subroutine runs again, it creates a new copy of the variable
and its value is reset. If the variable is declared with the Static keyword, however, the same instance of the variable is used whenever the subroutine runs. That means the variable’s
value is preserved between calls to the subroutine.
For example, a variable declared within a subroutine has scope equal to the subroutine. Code outside of the subroutine cannot access the variable. If a variable is declared on a module level outside any subroutine, it has module scope. If it is declared with the Private keyword, it is accessible only to code within the module. If it is declared with the Public keyword, then it is also accessible to code outside of the module.
Visibility is a concept that combines scope, accessibility, and lifetime. It determines whether a certain piece of code can use a variable. If the variable is accessible to the code, the code is within the variable’s scope, and the variable is within its lifetime (has been created and not yet destroyed), then the variable is visible to the code.
Variables are among the most fundamental building blocks of a program. A variable is a program object that stores a value. The value can be a number, letter, string, date, structure containing other values, or an object representing both data and related actions.
When a variable contains a value, the program can manipulate it. It can perform arithmetic operations on numbers, string operations on strings (concatenation, calculating substrings,
f nding a target within a string), date operations (f nd the difference between two dates, add a
time period to a date), and so forth.
Four factors determine a variable’s exact behavior:
➤ Data type determines the kind of data it can hold (integer, character, string, and so forth).
➤ Scope def nes the code that can access the variable. For example, if you declare a variable
inside a For loop, only other code inside the For loop can use the variable. If you declare a
variable at the top of a subroutine, all the code in the subroutine can use the variable.
➤ Accessibility determines what code in other modules can access the variable. If you declare
a variable at the module level (outside of any subroutine in the module) and you use the
Private keyword, then only the code in the module can use the variable. If you use the Public
keyword, then code in other modules can use the variable as well.
➤ Lifetime determines how long the variable’s value is valid. A variable inside a subroutine
that is declared with a normal Dim statement is created when the subroutine begins and is
destroyed when it exits. If the subroutine runs again, it creates a new copy of the variable
and its value is reset. If the variable is declared with the Static keyword, however, the same instance of the variable is used whenever the subroutine runs. That means the variable’s
value is preserved between calls to the subroutine.
For example, a variable declared within a subroutine has scope equal to the subroutine. Code outside of the subroutine cannot access the variable. If a variable is declared on a module level outside any subroutine, it has module scope. If it is declared with the Private keyword, it is accessible only to code within the module. If it is declared with the Public keyword, then it is also accessible to code outside of the module.
Visibility is a concept that combines scope, accessibility, and lifetime. It determines whether a certain piece of code can use a variable. If the variable is accessible to the code, the code is within the variable’s scope, and the variable is within its lifetime (has been created and not yet destroyed), then the variable is visible to the code.
No comments:
Post a Comment