vb.net 如何在 Visual Basic 中声明全局变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22738243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would I declare a global variable in Visual Basic?
提问by user3470747
I want to create a variable that can be used across multiple forms.
我想创建一个可以跨多个表单使用的变量。
It's going to be a temporary storage place for integers.
它将成为整数的临时存储位置。
回答by competent_tech
There are a couple of ways to do this in VB: a VB-specific way and a non-VB specific way (i.e. one that could also be implemented in C#.
在 VB 中有几种方法可以做到这一点:特定于 VB 的方式和非特定于 VB 的方式(即也可以在 C# 中实现的方式。
The VB-specific way is to create a module and place the variable in the module:
VB特有的方式是创建一个模块并将变量放在模块中:
Public Module GlobalVariables
Public MyGlobalString As String
End Module
The non-VB-specific way is to create a class with shared properties:
非 VB 特定的方法是创建一个具有共享属性的类:
Public Class GlobalVariables
Public Shared Property MyGlobalString As String
End Class
The primary difference between the two approaches is how you access the global variables.
这两种方法的主要区别在于您如何访问全局变量。
Assuming you are using the same namespace throughout, the VB-specific way allows you to access the variable without a class qualifier:
假设您始终使用相同的命名空间,VB 特定的方式允许您在没有类限定符的情况下访问变量:
MyGlobalString = "Test"
For the non-VB-specific way, you must prefix the global variable with the class:
对于非 VB 特定的方式,您必须使用类作为全局变量的前缀:
GlobalVariables.MyGlobalString = "Test"
Although it is more verbose, I strongly recommend the non-VB-specific way because if you ever want to transition your code or skillset to C#, the VB-specific way is not portable.
虽然它更冗长,但我强烈推荐非 VB 特定的方式,因为如果您想将代码或技能集转换为 C#,VB 特定的方式是不可移植的。
回答by C.P. Williams
You can just add it as PUBLIC to ANY Module
您可以将其作为 PUBLIC 添加到任何模块
Example:
例子:
Module Module1 'Global variables Public glbtxtTemplateName As String 'GLOBAL VARIABLE FOR TEMPLATE
Module Module1 '全局变量 Public glbtxtTemplateName As String 'GLOBAL VARIABLE FOR TEMPLATE
VB loads the Modals first as a class and all PUBLIC items therein are shared directly. Think about it this way.
VB 首先将 Modals 作为一个类加载,并且其中的所有 PUBLIC 项目都直接共享。这样想想。
Lets say we have a MODULE called "MY_PROCESSES"
假设我们有一个名为“MY_PROCESSES”的模块
When you declare a SUB or a FUNCTION in "MY_PROCESSES" if you want it to be used OUTSIDE of "MY_PROCESSES" you declare as PUBLIC like this
当您在“MY_PROCESSES”中声明 SUB 或 FUNCTION 时,如果您希望它在“MY_PROCESSES”之外使用,您可以像这样声明为 PUBLIC
PUBLIC SUB LOAD_TEMPLATE() ....
公共 SUB LOAD_TEMPLATE() ....
To get to LOAD_TEMPLATE you just call it in your code from anywhere:
要访问 LOAD_TEMPLATE,您只需从任何地方在代码中调用它:
LOAD_TEMPLATE
加载模板
So if I need to set or use the global variable that I made public in my module I just refer to it by name:
因此,如果我需要设置或使用我在模块中公开的全局变量,我只需按名称引用它:
glbtxtTemplateName="TEMPLATE_NAME"
glbtxtTemplateName="TEMPLATE_NAME"
IF glbtxtTemplateName="" then LoadTemplate
如果 glbtxtTemplateName="" 然后 LoadTemplate
I do like building the class as above because you can reference it faster without remembering the variable but if you only need 1 or 2 global variables you can name them like we used to with Hungarian Notation style name. This method is really quite simple and elegant. Old is new and New is Old.
我确实喜欢像上面那样构建类,因为您可以在不记住变量的情况下更快地引用它,但是如果您只需要 1 或 2 个全局变量,您可以像以前使用匈牙利符号样式名称一样命名它们。这个方法真的很简单也很优雅。旧的就是新的,新的就是旧的。