如何在VB.NET中全局声明结构变量

时间:2012-12-06 20:07:57

标签: vb.net

我在某个模块上声明了一个公共结构:

<Serializable()> Public Structure aSetup
    Dim check As Boolean
    Dim times As Double
    Dim sTimes As Integer
    Dim noone As String
End Structure

在以任何形式或模块使用它之前,我必须从这个结构中声明一个局部变量。

例如:

Dim asT as aSetup
asT.noone = "Nemo"

我想知道是否可以全局声明asT变量,因此可以通过所有文件实现结构数据。如果可能,我该如何以及在哪里这样做?

2 个答案:

答案 0 :(得分:3)

你需要将它放在静态类中 - 在VB.NET中称为Module

答案 1 :(得分:1)

创建附加到某个类的结构的Shared实例。类似的东西:

Public Class Settings
    Private Shared Property asTInitialized As Boolean = False
    Private Shared _asT As aSetup
    Public Shared ReadOnly Property asT As aSetup
        Get
            If Not asTInitialized Then
                _asT.noone = "Nemo"
                'other init code

                asTInitialized = True
            End If
        End Get
    End Property
End Class

然后在其他文件中通过Settings.asT访问它。

相关问题