Let's take the following case
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
My program instantiate Anexa3ViewModel
, then it sets Proprietari property
which sets Proprietar
to be Proprietari(0)
(when I debug, this seems to work correctly), then I call ToXML
by pressing a button through commanding. Inside ToXML
Anexa.proprietar
is nothing, but Anexa.Proprietari(0)
has the correct value.
Apparently proprietar
property lost its value, or there are two Proprietar
Properties stored, one for my base class and one for the derived class. I thought this is possible only by shadowing a base property, which I'm not doing. I think there is some inheritance notion that I fail to understand.
Could someone shed some light on this please?
Clarifications1: I know that Proprietari
's setter doesn't get fire if I simply add an item to the collection. This is not my problem as I set the whole collection at once and its setter gets fired and I can see that proprietar
gets the correct value of Proprietari(0)
. The problem is that it is loosing its value when it gets to ToXML
.