繁体   English   中英

共享/静态变量应该是虚无,不是-给出什么?

[英]Shared/Static Variable Should Be Nothing but Isn't - what gives?

我有以下代码:

Public Class TestClass
    Public Sub Main()
        If theGlobal IsNot Nothing Then Throw New Exception("What gives!")
    End Sub

    Private Shared theGlobal As Object = Nothing
    Private Shared ReadOnly Property Global
        Get
            If theGlobal Is Nothing Then
                theGlobal = New Object()
            End If

            Return theGlobal 
        End Get
    End Property
End Class

难过...为什么Global对象什么都不是?

假设这确实是您的代码,我想您是在带有断点的调试器中运行此代码,并且监视窗口正在评估该属性,该属性正在初始化theGlobal

只是基于我以前在类似情况下所见过的猜测。

如果那没有帮助,请尝试编写一个简短但完整的控制台应用程序,以重现该问题,并且我们所有人都可以运行。

我想你是说

If Global Is Nothing Then Throw New Exception("What gives!")

您必须根据代码访问要初始化的字段的Global属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM