Variable scope refer to the life of a variable and at where is may be used within a program.
For a sub routine or function a DIM statement is used to define a variable name and data type to a variable. Variables must be dimensioned before they can be used.
Procedural Level Scope means declared variables are declared and used just with in a Subroutine or function.
When an event handler is fired or a Sub/Function is called, the resources necessary to run that section of code are allocated. (This includes dimensioning variables) These resources exist as long as that section of code is running. Upon Exiting the Sub/function the resources are released back to the system for use which destroyes the variables.
(You can use the STATIC modifier to have the value of a variable remain and no get zeroed the next time the proceedure is called
To use a variable in more than one section of code a higher level of scope must be used. Basically this means having a variable properly declared outside of a proceedure (sub or function). A form level variable declares variables at the top of the form (These are also described as module level variableswhen declared in a module). Any proceedure written on the form below these declarations will have access to these variables. Form Level variables have a life as long as the form / module exists.
The top level of scope is program level. Variables which can be used any where any time and have a life as long as the program is running. These are declared in a module with the PUBLIC modifier. Variables declared in this manner are also known as GLOBAL.
Proper use of variable scope is key to writting reliable and easy to debug code. Misuse of variable scope can allow other sections of code to modifiy a variable in unintended/unexpected ways during the program run.
It is best to declare a variable with the lowest scope necessary. Typically this is Proceedural Scope
2007-11-11 00:23:37
·
answer #1
·
answered by MarkG 7
·
0⤊
0⤋
DIM myVariable as float (or int or whatever) Do yourself a favor and learn a real language like C# or Java. (DIM - comes from "dimension" which has no meaning anymore... it's a relic from the old days, and a completely unsuitable term for declaring a variable.) In SQL, you say "declare"... I declare, that makes a lot more sense. And no, you would use the "text" property of anything... you want a number, not a string. Visual Basic lets you be lazy and it will automatically convert that string to a number, but that allows the user to type something like "eighteen percent" which can not be converted to a number, causing your program to crash because of user input, which is a very bad thing. Weak typing is a very bad thing actually, and VB is the single biggest source of bad code in the world, mostly for that reason.
2016-04-03 07:14:15
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋