vb.net 声明全局、静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23384296/
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
Declaring global, static variables
提问by user3568182
I'm trying to set a global variable in Visual Studio, but I can't make it static. Is there any way for me to set the variable as static and share it across different methods, or some way to save the variable each time it changes?
我正在尝试在 Visual Studio 中设置一个全局变量,但我无法将其设为静态。有什么方法可以让我将变量设置为静态并在不同的方法中共享它,或者每次更改时都以某种方式保存变量?
回答by Matt Wilko
You have two options:
您有两个选择:
1 - Create a class that contains a Shared variable (this is the same a static variable in c#)
1 - 创建一个包含共享变量的类(这与 c# 中的静态变量相同)
Public Class GlobalVariables
Public Shared Bar As String
End Class
You can then access this using the class name:
然后您可以使用类名访问它:
GlobalVariables.Bar = "Hello world"
2 - Create a module (this is akin to a static class in c#)
2 - 创建一个模块(这类似于 c# 中的静态类)
Public Module GlobalVariables
Public Bar As String
End Module
You can then access this value in code like this:
然后,您可以在代码中访问此值,如下所示:
Bar = "Goodbye cruel world"
回答by Guru Josh
If you use the number 1 option presented by @Matt Wilko, you can reference the shared member either through an object instance of the class or by referencing the class without an object reference. Both point to and increment the same variable and therefore reference the same value. Although, the Visual Studio compiler provides a warning about referencing an object instance and says that it will not be evaluated, it still compiles. The compiler's recommendation is to use the class name.
如果您使用@Matt Wilko 提供的数字 1 选项,您可以通过类的对象实例或通过引用没有对象引用的类来引用共享成员。两者都指向并递增相同的变量,因此引用相同的值。尽管 Visual Studio 编译器提供了有关引用对象实例的警告并表示不会对其进行评估,但它仍然可以编译。编译器的建议是使用类名。
Public Class GlobalVariables
Public Shared Foo As Integer
End Class
Insert the following into a form and call IncrementIntegers() from a button click event procedure and you will find that myGlobalVariables.Foo and GlobalVariables.Foo both return 20.
将以下内容插入表单并从按钮单击事件过程调用 IncrementIntegers(),您会发现 myGlobalVariables.Foo 和 GlobalVariables.Foo 都返回 20。
Private Sub IncrementIntegers()
Dim myGlobalVariables As New GlobalVariables
myGlobalVariables.Foo = 0
GlobalVariables.Foo = 0
myGlobalVariables.Foo += 10
GlobalVariables.Foo += 10
Dim iLocalInt1 = myGlobalVariables.Foo
MessageBox.Show("myGlobalVariables.Foo = " & iLocalInt1.ToString)
Dim iLocalInt2 = GlobalVariables.Foo
MessageBox.Show("GlobalVariables.Foo = " & iLocalInt2.ToString)
End Sub
Note that with option 1, Foo must be qualified with either the class name or an object name. With option 2, it is a module and not a class so an object reference cannot be created. The public variable can be referenced without qualifying it with the module name unless a variable with the same name appears in another module in which case the compiler with throw a name conflict error. For example,
请注意,对于选项 1,必须使用类名或对象名来限定 Foo。对于选项 2,它是一个模块而不是一个类,因此无法创建对象引用。公共变量可以在不使用模块名称限定的情况下被引用,除非具有相同名称的变量出现在另一个模块中,在这种情况下编译器会抛出名称冲突错误。例如,
Public Module1
Public Foo As String
End Module
Public Module2
Public Foo As String
End Module
Remove Module2 and Foo can be called unqualified from anywhere.
Remove Module2 和 Foo 可以在任何地方被称为 unqualified。
Foo = "Happy birthday"
With Module2 present, Foo must be qualified with the name as both point to different variable and represent different and independent values.
在 Module2 存在的情况下,Foo 必须用名称限定,因为它们都指向不同的变量并代表不同的独立值。
Module1.Foo = "Goodbye cruel world"
Module2.Foo = "Hello new world"

