在 VB.net 中声明一个公共数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32584513/
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 a public array in VB.net
提问by DatBrummie
I know that to declare a string in VB I would use the line
我知道要在 VB 中声明一个字符串,我会使用该行
Dim ExString As String
And to declare a global string I'd use
并声明一个我会使用的全局字符串
Public Shared Property ExString As String
Which I'd access using (assuming it was saved in a class called GlobalVars)
我会使用它来访问(假设它保存在一个名为 GlobalVars 的类中)
MsgBox(GlobalVars.ExString)
I also know that to declare a string array it's
我也知道要声明一个字符串数组是
Dim ExString(3) As String
However declaring a public array doesn't seem to work the same, the line:
然而,声明一个公共数组似乎并不相同,这一行:
Public Shared Property ExString(3) As String
Doesn't seem to work.
I was wondering how I go about declaring a public array of strings in visual basic?
I'm using Visual Studio 2010 if that makes a difference.
似乎不起作用。
我想知道如何在 Visual Basic 中声明一个公共字符串数组?
如果这有所作为,我将使用 Visual Studio 2010。
Thanks in advance
提前致谢
采纳答案by Waescher
You cannot add the length (3) to your variable, because Visual Studio will nag:
您不能将长度 (3) 添加到您的变量中,因为 Visual Studio 会唠叨:
Identifier expected.
标识符预期。
But you can do something like:
但是您可以执行以下操作:
Public Shared Property MyString As String() = New String() { "abc", "def", "ghi"}
回答by DOTNET Team
If you want only one instance of the variable, you need a static member. Static members belong to the class, not an individual object. VB calls them Shared members because you can imagine the same variable is shared between all of the instances:
如果您只需要该变量的一个实例,则需要一个静态成员。静态成员属于类,而不是单个对象。VB 将它们称为共享成员,因为您可以想象所有实例之间共享相同的变量:
Public Class Form1
Public Shared ShuffleArray() As Integer
End Class
ReDim Form1.ShuffleArray(52)
Form1.ShuffleArray(0) = 10
Alternatively, you can create a module that contains the variable. Modules are a special type of class where two magic things happen. First, all of the members are in the global namespace so you don't need the module name to access them. Second, all members are automatically static.
或者,您可以创建一个包含该变量的模块。模块是一种特殊类型的类,其中发生了两件神奇的事情。首先,所有成员都在全局命名空间中,因此您不需要模块名称来访问它们。其次,所有成员都是自动静态的。
Module GlobalConstants
Public ShuffleArray() As Integer
End Module
ReDim ShuffleArray(51)
GlobalConstants.ShuffleArray(0) = 10
Console.WriteLine(ShuffleArray(0)) ' output : 10
I think this will serve your purpose in case you do not want to fix your 5 values.
我认为如果您不想修复您的 5 个值,这将有助于您的目的。

