使用属性时出现空引用异常
你看出问题出在哪里了吗?代码将从 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
,而应在 frmFacility
的 gotoday
子中设置为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有将
facilityForm
设置为任何内容,您只是声明了一个frmFacility
类型的变量并将其命名为facilityForm
。当您尝试调用facilityForm.IsOnPrimaryView
时,facilityForm
仍为null
。You didn't set the
facilityForm
to anything, you simply declared a variable of typefrmFacility
and called itfacilityForm
. When you attempt to callfacilityForm.IsOnPrimaryView
,facilityForm
is stillnull
.您实际上并没有使用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 aShared test as New frmFacility
.The Form can not have the IsOnPrimaryView property yet because it is not instanciated. It is practically
Nothing
您需要将 IsOnPrimaryView 属性设置为共享,否则您将得到 NullReference 异常。
You need to set IsOnPrimaryView property as shared, otherwise you will get NullReference exception.