Using Multiple Variables with the Same Name
To see how this works, insert two standard modules in a new project and draw three command buttons on a form.
One variable, intX, is declared in the first standard module, Module1. The Test procedure sets its value:
Public intX As Integer ' Declare Module1's intX.Sub Test()
' Set the value for the intX variable in Module1.
intX = 1
End Sub
The second variable, which has the same name, intX, is declared in the second standard module, Module2. Again, a procedure named Test sets its value:
Public intX As Integer ' Declare Module2's intX.Sub Test()
' Set the value for the intX variable in Module2.
intX = 2
End Sub
The third intX variable is declared in the form. module. And again, a procedure named Test sets its value.
Public intX As Integer ' Declare the form's intX' variable.
Sub Test()
' Set the value for the intX variable in the form.
intX = 3
End Sub
Each of the three command buttons' Click event procedures calls the appropriate Test procedure and uses MsgBox to display the values of the three variables.
Private Sub Command1_Click()Module1.Test ' Calls Test in Module1.
MsgBox Module1.intX ' Displays Module1's intX.
End Sub
Private Sub Command2_Click()
Module2.Test ' Calls Test in Module2.
MsgBox Module2.intX ' Displays Module2's intX.
End Sub
Private Sub Command3_Click()
Test ' Calls Test in Form1.
MsgBox intX ' Displays Form1's intX.
End Sub
Run the application and click each of the three command buttons. You'll see the separate references to the three public variables. Notice in the third command button's Click event procedure, you don't need to specify Form1.Test when calling the form's Test procedure, or Form1.intX when calling the value of the form's Integer variable. If there are multiple procedures and variables with the same name, Visual Basic takes the value of the more local variable, which in this case, is the Form1 variable.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11411056/viewspace-734287/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [Javascript] Import the Same JavaScript Module Multiple Times with Cache BustingJavaScriptImport
- CMake Error: add_executable cannot create target ““ because another target with the same name已解決Error
- django rest django.core.exceptions.ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view nameDjangoRESTExceptionView
- [Bash] Environment variables
- Task04 :Variables and FunctionsFunction
- 100-Same Tree
- 100. Same Tree
- [leetcode]same-treeLeetCode
- TensorFlow入門 - 變數(Variables)變數
- 17-Containers-Container Environment VariablesAI
- Leetcode 100. Same TreeLeetCode
- B. Same Parity Summands
- JavaScript select multipleJavaScript
- Logstash Multiple Pipelines
- variables_order引數詳解
- Leetcode 967 Numbers With Same Consecutive DifferencesLeetCode
- The fundamental idea remains the same as previous yearsIdeaREMAI
- c++11:std::is_sameC++
- Small Multiple(最短路)
- 2.3.6.2 Synchronization of Multiple ApplicationsAPP
- LLM multiple modal applicationsAPP
- kubernetes traefik multiple namespacesnamespace
- MySQL 索引優化 Using where, Using filesortMySql索引優化
- Different AG groups have the exactly same group_id value if the group names are same and the ‘CLUSTER_TYPE = EXTERNAL/NONE’None
- PostgreSQL DBA(129) - Extension(pg_variables).mdSQL
- MySQL explain結果Extra中"Using Index"與"Using where; Using index"區別MySqlAIIndex
- Multiple Books多賬薄
- POJ1426-Find The Multiple
- WPF筆記3——x:Name 與 Name筆記
- Task 04 變數與函式 Variables and Functions變數函式Function
- Oracle修改instance_name、db_name、db_unique_name、service_namesOracle
- Using hints for PostgresqlSQL
- String interpolation using $
- using的用法
- Using mysqldump for backupsMySql
- MySQL 之 USINGMySql
- Lowest Common Multiple Plus hd 2028
- [20210218]Select vs Assign – How To Assign PLSQL Variables.txtSQL
- [20190706]Same dog, different leash – functions in SQL.txtFunctionSQL