开发者

A Null Refrence Exception while working with Properties

开发者 https://www.devze.com 2023-02-10 10:00 出处:网络
Do you see where the problem lies? Code will run from frmFacility and will shift into UserControl: Public Class frmFacility

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 n开发者_开发技巧eed 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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号