L
Lino Barreca
Take a look at this code:
Class clsAnagrafica
Public Overridable ReadOnly Property Codice() As Integer
Get
Return 1
End Get
End Property
End Class
Class clsCliente
Inherits clsAnagrafica
Public Overrides ReadOnly Property Codice() As Integer
Get
Return 2
End Get
End Property
End Class
Class clsDocumento
Public Anagrafica As New clsAnagrafica
End Class
Class clsDocumentoCliente
Inherits clsDocumento
Public Shadows Anagrafica As New clsCliente
End Class
Module Module1
Sub Main()
Dim A As clsAnagrafica
A = New clsCliente
Debug.WriteLine(A.Codice)
Dim D As clsDocumento
D = New clsDocumentoCliente
Debug.WriteLine(D.GetType)
Debug.WriteLine(D.Anagrafica.Codice)
End Sub
End Module
A.Codice returns 2 (because of Overridden member).
D.GetType obviously returns clsDocumentoCliente.
But the strange thing is that D.Anagrafica.Codice returns 1.
I can't understand why!
clsDocumentoCliente.Anagrafica shadows clsDocumento.Anagrafica
so the compiler should call clsDocumentoCliente.Anagrafica.Codice. (as MSDN
states "the same base method can perform different actions depending on the
run-time type of the instance that invokes the method") and the run-time
type is clsDocumentoCliente.
However I find that this behaviour is somewhat related to the initial
declaration of D.
Infact if I declare D as object D.Anagrafica.Codice returns 2. (as it should
be)
Can anyone explain me why this happens?
Thanks in advance, Lino.
Class clsAnagrafica
Public Overridable ReadOnly Property Codice() As Integer
Get
Return 1
End Get
End Property
End Class
Class clsCliente
Inherits clsAnagrafica
Public Overrides ReadOnly Property Codice() As Integer
Get
Return 2
End Get
End Property
End Class
Class clsDocumento
Public Anagrafica As New clsAnagrafica
End Class
Class clsDocumentoCliente
Inherits clsDocumento
Public Shadows Anagrafica As New clsCliente
End Class
Module Module1
Sub Main()
Dim A As clsAnagrafica
A = New clsCliente
Debug.WriteLine(A.Codice)
Dim D As clsDocumento
D = New clsDocumentoCliente
Debug.WriteLine(D.GetType)
Debug.WriteLine(D.Anagrafica.Codice)
End Sub
End Module
A.Codice returns 2 (because of Overridden member).
D.GetType obviously returns clsDocumentoCliente.
But the strange thing is that D.Anagrafica.Codice returns 1.
I can't understand why!
clsDocumentoCliente.Anagrafica shadows clsDocumento.Anagrafica
so the compiler should call clsDocumentoCliente.Anagrafica.Codice. (as MSDN
states "the same base method can perform different actions depending on the
run-time type of the instance that invokes the method") and the run-time
type is clsDocumentoCliente.
However I find that this behaviour is somewhat related to the initial
declaration of D.
Infact if I declare D as object D.Anagrafica.Codice returns 2. (as it should
be)
Can anyone explain me why this happens?
Thanks in advance, Lino.