【问题标题】:Inherited Property is nothing inside a method of the base class继承的属性在基类的方法中什么都不是
【发布时间】:2013-07-03 07:06:31
【问题描述】:

我们来看下面的案例

Public MustInherit Class AnexaClass
    Inherits ObjectBase
    Private _proprietar As New ProprietarClass
    Public Property proprietar As ProprietarClass
        Get
            Return _proprietar
        End Get
        Set(value As ProprietarClass)
            _proprietar = value
            OnPropertyChanged("proprietar")
        End Set
    End Property
End Class

Public Class Anexa3Class
    Inherits AnexaClass

    Private _Proprietari As New ObservableCollection(Of ProprietarClass)
    Public Property Proprietari As ObservableCollection(Of ProprietarClass)
        Get
            Return _Proprietari
        End Get
        Set(value As ObservableCollection(Of ProprietarClass))
            _Proprietari = value
            OnPropertyChanged("Proprietari")
            If _Proprietari.Count > 0 Then
                Me.proprietar = _Proprietari(0) 'this line sets Proprietar to be the same as the first item of Proprietari and it works as it should be
            End If
        End Set
    End Property

Public MustInherit Class AnexaViewModel(Of AnexaT As {AnexaClass, New})
    Inherits ObjectBase

    Private _Anexa As New AnexaT
    Public Property Anexa As AnexaT
        Get
            Return _Anexa
        End Get
        Set(value As AnexaT)
            _Anexa = value
            OnPropertyChanged("Anexa")
        End Set
    End Property
    Public Sub ToXML()
        MsgBox(Anexa.proprietar.nume) 'at this point Anexa.proprietar is nothing
        MsgBox(Anexa.Proprietari(0).nume) ' but this is fine, even though Proprietari is only declared inside the derived class Anexa3Class
        ''Some other code''
    End Sub
End Class
Public Class Anexa3ViewModel
    Inherits AnexaViewModel(Of Anexa3Class)
End Class

我的程序实例化Anexa3ViewModel,然后将Proprietari property 设置为Proprietar 设置为Proprietari(0)(当我调试时,这似乎工作正常),然后我通过命令按下按钮调用ToXML . ToXML Anexa.proprietar 里面什么都没有,但 Anexa.Proprietari(0) 有正确的值。

显然proprietar 属性丢失了它的值,或者存储了两个Proprietar 属性,一个用于我的基类,一个用于派生类。我认为这只能通过遮蔽基本属性来实现,而我没有这样做。我认为有一些我无法理解的继承概念。

有人能解释一下吗?

Clarifications1:我知道Proprietari 的setter 不会被触发,如果我只是将一个项目添加到集合中。这不是我的问题,因为我一次设置了整个集合并且它的设置器被触发,我可以看到proprietar 获得了正确的值Proprietari(0)。问题是当它到达ToXML 时,它正在失去它的价值。

【问题讨论】:

  • 请不要在标题中添加标签信息。这里的标签系统非常擅长对事物进行分类,不需要帮助。 :-) 请参阅Should questions include "tags" in their titles?。谢谢。
  • 注意到了。至少我正确使用了标签。

标签: vb.net inheritance properties


【解决方案1】:

没有看到应用程序的其余部分,最好的猜测是您有其他代码向 Anexa3Class 中的 Proprietari 集合添加值,而不是通过属性设置整个集合。

对此有一些潜在的解决方法,具体取决于您的应用程序需求和设计。一种方法是处理 Proprietari 集合上的 CollectionChanged 事件:

Private WithEvents _Proprietari As New ObservableCollection(Of ProprietarClass)
Private Sub _Proprietari_CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles _Proprietari.CollectionChanged
    If Me.proprietar Is Nothing AndAlso e.Action = Specialized.NotifyCollectionChangedAction.Add Then
        Me.proprietar = e.NewItems(0)
    End If
End Sub

另一个可能的选择是将 propietar 更改为可覆盖的只读属性并始终返回第 0 个值,但这可能会破坏 OnPropertyChanged 逻辑。

【讨论】:

  • 您回答了我未提出的问题之一。每当我将项目添加到proprietari 时,我都需要一种更新 proprietar 的方法,并且我现在正在研究 CollectionChanged,但这并不是所说的实际问题。我不明白为什么将项目添加到 Proprietari 集合会改变 proprietar 值,一旦它被设置,当 Proprietari 集合被设置时(我实际上是通过反序列化将它设置为一个整体)。所以我的问题不是为什么 proprietar 不更新,而是为什么它的值在 ToXML 方法中没有任何其他代码可以更改 proprietar 值。
  • @Andrei:我认为问题在于,在您设置了 Proprietari 集合之后, proprietar 被设置为 Nothing。如果您通过反序列化设置属性,那么问题很可能是在对象之前设置集合(即设置 Proprietari 然后设置 Proprietar)。根据重现的难度,我会做两件事之一:1)在调试模式下,在专有设置中设置一个断点,然后查看它何时设置为 Nothing; 2)如果无法调试,则在setter中添加代码以测试该值并在Nothing时抛出异常,然后使用堆栈跟踪。
  • 你是对的,问题是反序列化。我将编辑您的回复并将其标记为已回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 2015-05-08
  • 1970-01-01
  • 2021-01-01
  • 2017-07-27
  • 2021-05-07
  • 2015-07-15
相关资源
最近更新 更多