类属性...是在类的每个实例化时调用的GET

时间:2016-10-21 14:15:34

标签: vb.net class instantiation

我有一个类“设置”,它是我的应用程序中针对每个用户设置的全局可用属性集。它是通过将用户字符串传递到New Sub例程来创建的,该例程非常简化为:

Class Settings
    Property This as string
    Property That as string
    Sub New(User as string)
        ' Get new things from the database and set the properties
    End Sub
End Class

我有一个属性Websites,它是Website个对象的集合,因此我的类的更完整模型是

Class Settings
    Property This as string
    Property That as string
    Property Websites as List(of Website)
    Sub New(User as string)
        ' Get new things from the database and set the properties
    End Sub
    Class Website
        Property Domain as string
        '.... more properties
    End Class
End Class

获取网站属性需要一些时间,因此对于性能,我希望仅在需要时填充此属性。

所以我写下这个属性如下:

ReadOnly Property Websites() As List(Of Website)
    Get
        If Me.UserKey = "" Then ' Checks if the Parent Class `Settings` exists as an object and has a UserKey
            Throw New Exception("Cannot Fetch Website Objects with no valid Settings Object.")
        End If
        Dim w As List(Of Website) = ' Go to the database and make website objects 
        Return w
    End Get
End Property

我的问题是......每次调用New Settings(User)时都会填充此属性,从而无法实现我的目的,或者它会保留null直到我需要它并在代码中使用它,例如< / p>

Dim Settings as New Settings(User)
Dim MyWebsiteList as List(of Website) = Settings.Websites
'   Do things with the websites

或者......我发现这一切都错了,应该在设置类中创建一个函数并将用户引用传递给...

0 个答案:

没有答案