Ryan,
You don't really need to use Shadows here, its adding to your confusion!
Public Shadows Class ClassB
I would simply make it ClassD
The first problem you are having is when you create a ClassA object it
automatically creates a ClassA.ClassB object, you need to let ClassA &
ClassC decide what member to create. I would use constructors for this.
ClassA should define a protected constructor that allows you to set the
nested variable, both ClassA & ClassC should define public default
constructors that call the protected constructor with an instance of the
correct nested type.
Try something like:
Public Class ClassA
Public ReadOnly nestedClassB As ClassB
Public Sub New()
MyClass.New(New ClassB)
End Sub
Protected Sub New(ByVal nested As ClassB)
nestedClassB = nested
End Sub
Public Class ClassB
Public Inner1 As String
End Class
End Class
Public Class ClassC
Inherits ClassA
Public Sub New()
MyBase.New(New ClassD)
End Sub
Public Class ClassD
Inherits ClassA.ClassB
Public Inner2 As String
End Class
End Class
The second problem that you are having is that ClassA has defined a public
variable of type ClassA.ClassB. ClassA.ClassB does not have a Inner2 field,
ClassC.ClassD has the Inner2 field. You will need to cast the nestedClassB
variable to ClassC.ClassD to see Inner2. However casting is not polmorphic.
If you need to access Inner2 from ClassA a lot, you may want to add an
overridable property of ClasssB that ClassD can implement
Public Class ClassB
Public Inner1 As String
Public Overridable Property Inner2 As String
Get
' I would either throw an exception or return a
reasonable default
' depending on the desired results.
Throw New NotImplementedException
End
Set(value As String)
' I would either throw an exception or return a
reasonable default
Throw New NotImplementedException
End Set
End Property
End Class
Public Class ClassD : Inherits ClassA.ClassB
Public Inner1 As String
Private m_inner2 As String
Public Overrides Property Inner2 As String
Get
Return m_inner2
End
Set(value As String)
m_inner2 = value
End Set
End Property
End Class
Hope this helps
Jay
Ryan Shaw said:
Well, I'm still a little confused if what I'm trying to do is considered
multi threading or posible. Here is a more detailed example: In my real
issue, I have the public members as properties. When I use ClassB, the
second public member does not show up.