Problem with Nested Classes and Inheritance

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m having a small problem with inheritance with a hierarchy of classes
The example is

Class Class
Private m_classB as Class

Class Class
End Clas
End Clas

Class Class
Inherits Class
End Clas

How do I add properties to ClassB within ClassC
 
In this case, ClassB is a nested class of ClassA. So what to do depends on
what desired effect you want.
If what you're looking for is a way to access m_classB from ClassC, just
make it Protected instead of Private in ClassA

If you're trying to extend ClassB from ClassC, that's not going to happen.
The best you can hope for is creating another nested class in ClassC that
inherits from ClassB.

-Rob Teixeira [MVP]
 
Hi Ryan,

In my opinion is m_classB an object instanced from Class B

I get the idea that you try to make multi inherriting in a clever way.

As far as I can see in your example is Class B from the same order as Class
A.

And multi inherriting is not possible in VB.net.

I hope this helps

Cor
 
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

Public Class Class
Public nestedClassB As ClassB = New Class

Public Class Class
Public Inner1 As Strin
End Clas
End Clas

Public Class Class
Inherits Class

Public Shadows Class Class
Inherits ClassA.Class
Public Inner2 As Strin
End Clas
End Class
 
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.

Which ClassB do you use: ClassA.ClassB or ClassC.ClassB? How do you use
ClassB? Where does the member not show up?
Public Class ClassA
Public nestedClassB As ClassB = New ClassB

Public Class ClassB
Public Inner1 As String
End Class
End Class

Public Class ClassC
Inherits ClassA

Public Shadows Class ClassB
Inherits ClassA.ClassB
Public Inner2 As String
End Class
End Class


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
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.
 
Back
Top