在VB.net中声明一个公共数组

时间:2021-07-11 01:48:48

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

谢谢提前

2 个解决方案

#1


0  

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"}

#2


0  

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个价值观。

#1


0  

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"}

#2


0  

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个价值观。