为什么静态不能在这个延迟加载代码中按预期工作?

时间:2016-08-01 15:32:13

标签: vb.net dictionary

代码很简单。它懒得加载字典。如果字典什么都没有,那么它就填充了字典。

然而,当我浏览程序时,If _countryDictionary Is Nothing Then内的代码被多次调用。我想知道出了什么问题?

   Get
        Static _countryDictionary As Generic.Dictionary(Of String, String)



        If _countryDictionary Is Nothing Then
            _countryDictionary = New Generic.Dictionary(Of String, String)

            Dim listOfCountries = fileToCol(COUNTRYCODESFileName)

            For Each var In listOfCountries
                Dim ar = var.Split({"*"}, System.StringSplitOptions.None).ToList()
                _countryDictionary.Add(LCase(ar(0)), UCase(ar(1)))
            Next
            _countryDictionary.Add("delete", "de")
            _countryDictionary.Add("default", "df")
            _countryDictionary.Add("pakinmay", "py")

        End If

        Return _countryDictionary(country)
    End Get

enter image description here 这是我的调试截图。如你所见,它仍然没有。静态关键字在vb.net中的get方法上的工作方式是否有所不同?

更新:根据答案,似乎这里的静态变量对于类的不同实例是不同的。我一直认为单词static意味着变量不在堆中,而是在堆栈中或代码部分中。我想我错了。

3 个答案:

答案 0 :(得分:2)

正如@Mark在他的评论和文档中指出的那样,Static关键字定义了在定义静态变量的过程的不同调用之间共享的变量(在你的情况下为Getter)

Public Class Test

    Public ReadOnly Property Value As Integer
        Get
            Static SomeConstant As Integer = 0
            SomeConstant += 1
            Return SomeConstant
        End Get
    End Property

End Class

Sub Main()

  Dim test1 As New Test()
  Console.WriteLine(test1 .Value) 'Print 1
  Console.WriteLine(test1 .Value) 'Print 2

  Dim another As New Test()
  Console.WriteLine(another.Value) 'Print 1
  Console.WriteLine(another.Value) 'Print 2

End Sub

所以在你的情况下,getter countryCode是从你班级的不同实例执行的。

如果要在类的所有实例之间共享实例,请使用关键字Shared

创建静态成员
Public Class YourClass

    Private Shared ReadOnly CountryDictionary As Dictionary(Of String, String)

    Public ReadOnly Property CountryCode As String
        Get
            Return YourClass.CountryDictionary("country")
        End Get
    End Property

End Class

然后以与使用_countryDictionary变量

相同的方式使用它

答案 1 :(得分:1)

更好的替代方法是使用shared关键字,类似于c#中的static。这将创建一个“全局”变量,该类可以被该类的所有实例访问。这可能就是你想要的了。

Shared _countryDictionary As Generic.Dictionary(Of String, String)

Get
    If _countryDictionary Is Nothing Then
        _countryDictionary = New Generic.Dictionary(Of String, String)
        ... populate dictionary

请参阅Shared (visual basic)

答案 2 :(得分:0)

我将代码更改为

Private Shared Function countryDictionary() As Generic.Dictionary(Of String, String)
    Static _countryDictionary As Generic.Dictionary(Of String, String)
    If _countryDictionary Is Nothing Then
        _countryDictionary = New Generic.Dictionary(Of String, String)

        Dim listOfCountries = fileToCol(COUNTRYCODESFileName)

        For Each var In listOfCountries
            Dim ar = var.Split({"*"}, System.StringSplitOptions.None).ToList()
            _countryDictionary.Add(LCase(ar(0)), UCase(ar(1)))
        Next
        _countryDictionary.Add("delete", "de")
        _countryDictionary.Add("default", "df")
        _countryDictionary.Add("pakinmay", "py")

    End If
    Return _countryDictionary

End Function

Public ReadOnly Property countryCode As String
    Get
        Dim _countryDictionary = countryDictionary()

        Return _countryDictionary(country)
    End Get
End Property

这是对其他答案的补充。基本上我将计算量很大的代码部分移动到了一个延迟加载的共享函数。那些需要使用某些成员变量的人只能调用延迟加载函数。