VB variables, constants and data types and process overview (ii)

Source: Internet
Author: User
Tags integer

Variables used inside a module
By default, module-level variables are available to all procedures for the module, but the code for other modules is unavailable. Module-level variables can be established at the declaration segment at the top of the module using the Private keyword to declare the module-class variable. For example:
Private IntTemp as Integer
At the module level, there is no difference between private and Dim, but private is better because it is easy to differentiate it from public and make the code easier to understand.

Variables used by all modules
To make module-level variables valid in other modules, declare variables with the Public keyword. The values in a public variable can be used in all procedures of the application. As with all module-level variables, the public variables are declared at the declaration segment at the top of the module. For example:
Public IntTemp as Integer
Note You cannot declare a common variable in a procedure, you can only declare a common variable in a module's declaration section.
For more information about variables, see advanced variable topics.

Advanced Variable Topics

Use multiple variables with the same name
if common variables in different modules use the same name, you can differentiate them in your code by referencing both the module name and the variable name. For example, if you have a common Integer variable IntX that is declared in both Form1 and Module1, you will get the correct value by referencing them as Module1.intx and Form1.intx.
to see how this works, insert two standard modules into a new project and draw three command buttons on the form. The
declares a variable IntX in the first standard module MODULE1. The Test procedure sets its value:
Public IntX as Integer ' declares Module1 IntX.
Sub Test ()
Sets the value of the IntX variable for Module1. The
IntX = 1
End Sub
Declares the second variable IntX in the second standard module Module2, which has the same name. Another procedure named Test sets its value:
Public IntX as Integer ' declares the IntX of Module2.
Sub Test ()
Sets the value of the IntX variable for Module2. The
IntX = 2
End Sub
declares a third variable IntX in a form module. The procedure named Test sets its value again. The
public IntX as Integer declares the IntX variable for the form.
Sub Test ()
Sets the value of the IntX variable in the form.
IntX = 3
End Sub
During the Click event procedure for three command buttons, each call the corresponding Test procedure and use MsgBox to display the values of the three variables.
Private Sub Command1_Click ()
Module1.test ' invokes Test in Module1. The
MsgBox module1.intx ' shows the IntX of the Module1.
End Sub

Private Sub Command2_Click ()
Module2.test ' invokes Test in Module2.
MsgBox Module2.intx ' shows the Module2 IntX.
End Sub

Private Sub Command3_Click ()
Test ' invokes test in Form1.
MsgBox IntX ' shows the Form1 IntX.
End Sub
Run the application and click each button in the three command buttons. You will see that three common variables are referenced separately. Note that in the Click event procedure for the Third command button, you do not have to specify a form1.test when you call the Form1 Test procedure, and you do not have to specify FORM1.INTX when you call the value of the Form1 Integer variable. If more than one procedure or variable has the same name, the VisualBasic takes a more restricted value, in this case the FORM1 variable.

Comparison of common variables and local variables
Variables with the same name can also be in different ranges. For example, you can name a public variable of temp, and then declare a local variable named temp in the procedure. The local variable is accessed by referencing the name temp in the procedure, and the public variable is accessed by referencing the name temp outside the procedure. You can access such variables within a procedure by qualifying a module-level variable with the module name.
Public Temp as Integer
Sub Test ()
Dim Temp as Integer
The value of temp = 2 ' temp is 2.
The value of MsgBox form1.temp ' form1.temp is 1.
End Sub

Private Sub Form_Load ()
Temp = 1 ' Sets the value of Form1.temp to 1.
End Sub
Private Sub Command1_Click ()
Test
End Sub
Generally speaking, when the variable name is the same and the scope is not the same, the limited variables always use "shadow" to cover the less limited variables (that is, priority access to the limitations of large variables). Therefore, if it is also known as a procedure-level variable of temp, it hides the public variable temp inside the module with shadow.

Shadow Form properties and controls
Because of the shadow effect, form properties, controls, constants, and procedures are treated as module-level variables in the form module. The name of a form property or control that is the same as the name of a module-level variable, constant, custom type, or procedure is illegal because they are of the same scope.
In a form module, a local variable with the same name as a control in a form obscures the control with the same name. You must either reference the form name or the ME keyword to qualify the control to set or get the value of the control or its property value. For example:
Private Sub Form_Click ()
Dim Text 1, BackColor
' Suppose the form has a control that is also called TEXT1.
Text1 = "Variable" variable hides the control with shadow.
ME.TEXT1 = "Control" to get controls, must be qualified with ' Me '.
Text1.top = 0 ' cause an error!
Me.Text1.Top = 0 ' to get the control, it must be qualified with ' Me '.
BackColor = 0 ' variable hides the attribute with shadow.
Me.backcolor = 0 ' To get form properties, you must qualify with ' Me '.
End Sub

Use variables and procedures with the same name
The name of a private module-level variable and a public module-level variable can also conflict with the procedure name. A variable in a module cannot have the same name as any procedure or the type defined in the module. You can, however, have the same name as a type or variable defined in a common procedure or other module. In this case, when accessing the variable from another module, it must be qualified with the module name.
Although the above discussion of shadow rules is not complicated, a shaded approach can cause trouble and can result in difficult to find errors. Therefore, it is a good programming habit to use different names for different variables. You should try to make the variable name different from the name of the control in the form in the form module.

static variables
In addition to scope, variables also have lifetimes, during which the variables retain their value. The values of module-level and public variables are maintained throughout the lifetime of the application. However, for local variables declared by Dim and procedures for declaring local variables, these local variables exist only if the procedure is executing. Typically, when a procedure is executed, the value of its local variable is no longer present, and the memory occupied by the variable is freed. When the procedure is executed the next time, all its local variables are reinitialized.
However, you can define a local variable as static, thus preserving the value of the variable. Declaring one or more variables within a procedure with the Static keyword is exactly the same as the Dim statement:
Static Depth
For example, the following function adds the previous operating value stored in the static variable accumulate to a new value in order to calculate the total value of the operation.
Function runningtotal (num)
Static ApplesSold
ApplesSold = applessold + num
RunningTotal = ApplesSold
End Function
If you use Dim instead of a Static declaration of ApplesSold, the previous cumulative value is not preserved by calling the function, and the function simply returns the same value that called it.
The same effect is achieved by declaring ApplesSold in the module's declaration segment and making it a module-level variable. However, once this method changes the scope of the variable, the process is no longer exclusive access to the variable. Because other processes can also access and change the values of variables, the total value of the operation may be unreliable and the code will be more difficult to maintain.

Declare all local variables to be static variables
In order for all local variables in the procedure to be static variables, you can add the static keyword at the beginning of the procedure header. For example:
Static Function runningtotal (num)
This causes all local variables in the procedure to become static, whether they are declared with static, Dim, or private, or implicitly. You can place the Static in front of any Sub or Function procedure header, including the event procedure and the procedure declared Private.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.