473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem with Nested Classes and Inheritance

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

Nov 20 '05 #1
7 2390
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]

"Ryan Shaw" <an*******@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
I'm having a small problem with inheritance with a hierarchy of classes.
The example is:
Class Class A
Private m_classB as ClassB

Class ClassB
End Class
End Class
Class ClassC
Inherits ClassA
End Class

How do I add properties to ClassB within ClassC?

Nov 20 '05 #2
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]

"Ryan Shaw" <an*******@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
I'm having a small problem with inheritance with a hierarchy of classes.
The example is:
Class Class A
Private m_classB as ClassB

Class ClassB
End Class
End Class
Class ClassC
Inherits ClassA
End Class

How do I add properties to ClassB within ClassC?

Nov 20 '05 #3
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
Nov 20 '05 #4
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
Nov 20 '05 #5
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
Nov 20 '05 #6
"Ryan Shaw" <an*******@discussions.microsoft.com> schrieb
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

Nov 20 '05 #7
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" <an*******@discussions.microsoft.com> wrote in message
news:A8**********************************@microsof t.com... 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 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

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stephane Ninin | last post by:
Hello all, I am trying to play with nested class in a script I am making, and I am not sure I really understand how they work. Here is some code: __all__ =
10
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello...
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
13
by: Markus Ernst | last post by:
Hello In a content administration tool I call classes from inside classes in order to separate the admin functions from the display-only functions: class book { var $title; var $author;...
3
by: Luis Diego Fallas | last post by:
Hi everyone , I'm having a problem when trying to compile code that contains the following pattern: using System; public class A { public class B : Inn.C { }
4
by: Christopher Ireland | last post by:
Hi -- I'm trying to find an example of a nested class implemented within the .NET Framework itself but without much success. I don't suppose that anybody knows of such an example off the top of...
11
by: C# Learner | last post by:
Is it not possible to declare a nested class in a seperate file from its "parent" class -- i.e. in a similar way to the idea of spreading namespaces over more than one file?
3
by: Martin Skou | last post by:
I'm experimenting with using Python for a small web interface, using Mark Hammond's nice win32 extensions. I use a small class hierarchy which uses inheritance and nested classes. Here are a...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.