使用属性时出现空引用异常

发布于 2024-10-17 21:43:13 字数 1459 浏览 1 评论 0原文

你看出问题出在哪里了吗?代码将从 frmFacility 运行,并将转移到 UserControl:

Public Class frmFacility
Private primaryBaseDay As Date
Private isClassPrimaryView As Boolean = False
Friend WithEvents BookCtrl As ucBookCtrl2
Public Property Primary_BaseDay() As Date
    Get
      Return primaryBaseDay
    End Get
    Set(ByVal value As Date)
      primaryBaseDay = value
    End Set
  End Property

  Public Property IsOnPrimaryView() As Boolean
    Get
      Return isClassPrimaryView
    End Get
    Set(ByVal value As Boolean)
      isClassPrimaryView = value
    End Set
  End Property
Public Sub GotoDay(ByVal theDay As Date)
    Primary_BaseDay = theDay
    IsOnPrimaryView = True
    BookCtrl.GotoDay(theDay)
End Sub
End Class

   Imports frmFacility
Public Class ucBookCtrl2
Public Sub GotoDay(ByVal whichDay As Date, Optional ByVal MainFacilityUsed As String = "")
Dim facilityForm As frmFacility
If facilityForm.IsOnPrimaryView Then
        moDoBooking.m_BaseDay = facilityForm.Primary_BaseDay
        moDoBooking.m_CurrentDay = whichDay
        ShowDay()
        RaiseEvent ChangeOfDay()
End Sub
End Class

IffacilityForm.IsOnPrimaryViewThen 行,我得到一个 NullReferenceException。你知道原因吗?

另外,我无法创建 facilityForm 的新实例,因为我需要使用它的单例,尽管当我添加 frmFacility 的新实例时,IsOnPrimaryMode 设置为 false,而应在 frmFacilitygotoday 子中设置为 true

Do you see where the problem lies? Code will run from frmFacility and will shift into UserControl:

Public Class frmFacility
Private primaryBaseDay As Date
Private isClassPrimaryView As Boolean = False
Friend WithEvents BookCtrl As ucBookCtrl2
Public Property Primary_BaseDay() As Date
    Get
      Return primaryBaseDay
    End Get
    Set(ByVal value As Date)
      primaryBaseDay = value
    End Set
  End Property

  Public Property IsOnPrimaryView() As Boolean
    Get
      Return isClassPrimaryView
    End Get
    Set(ByVal value As Boolean)
      isClassPrimaryView = value
    End Set
  End Property
Public Sub GotoDay(ByVal theDay As Date)
    Primary_BaseDay = theDay
    IsOnPrimaryView = True
    BookCtrl.GotoDay(theDay)
End Sub
End Class

   Imports frmFacility
Public Class ucBookCtrl2
Public Sub GotoDay(ByVal whichDay As Date, Optional ByVal MainFacilityUsed As String = "")
Dim facilityForm As frmFacility
If facilityForm.IsOnPrimaryView Then
        moDoBooking.m_BaseDay = facilityForm.Primary_BaseDay
        moDoBooking.m_CurrentDay = whichDay
        ShowDay()
        RaiseEvent ChangeOfDay()
End Sub
End Class

At the line If facilityForm.IsOnPrimaryView Then, I get a NullReferenceException. Do you know the reason?

Also, I cannot create a new instance of the facilityForm, since I need to work with its singleton, although when I add a new instance of frmFacility, the IsOnPrimaryMode is set to false while it should be set to true in the gotoday sub from frmFacility.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

差↓一点笑了 2024-10-24 21:43:14

您没有将 facilityForm 设置为任何内容,您只是声明了一个 frmFacility 类型的变量并将其命名为 facilityForm。当您尝试调用 facilityForm.IsOnPrimaryView 时,facilityForm 仍为 null

You didn't set the facilityForm to anything, you simply declared a variable of type frmFacility and called it facilityForm. When you attempt to call facilityForm.IsOnPrimaryView, facilityForm is still null.

貪欢 2024-10-24 21:43:14

您实际上并没有使用facilityForm 单例。您必须将其存储在某个地方。也许是一个带有共享测试作为New frmFacility公共类

该表单尚不能具有 IsOnPrimaryView 属性,因为它尚未实例化。它实际上是什么都没有

You're not actually working with the facilityForm singleton. You will have to store it somewhere. Maybe a Public Class with a Shared test as New frmFacility.

The Form can not have the IsOnPrimaryView property yet because it is not instanciated. It is practically Nothing

丢了幸福的猪 2024-10-24 21:43:14

您需要将 IsOnPrimaryView 属性设置为共享,否则您将得到 NullReference 异常。

You need to set IsOnPrimaryView property as shared, otherwise you will get NullReference exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文