OOP question VB.NET

G

Guest

Can anyone tell me how to access a variable in a parent class from within a nested class?
Note: this snippet is only to illustrate my problem... I realize there's no need to get at the parent name THROUGH the child object, but it quickly shows what I'm after... any help would be appreciated. THANKS IN ADVANCE!

see example....

Public Class Parent

Private mParentName As String
Private mChild As Child

Public ReadOnly Property Name() As String
Get
Return mChild.ParentName
End Get
End Property


Sub New()
mParentName = "parent name test"
mChild = New Child
End Sub




Private Class Child

Public ReadOnly Property ParentName() As String
Get
'how can the child object be innately aware
'of a property of its parent object???
Return "???"
End Get
End Property

Friend Sub New()
MyBase.New()
End Sub

End Class


End Class
 
M

Marina

You could make a constructor to Child, that requires that the current Parent
be passed in, then store it in some variable. Something like:

Private myParent as Parent
Sub New(theParent as Parent)
myParent = theParent
End Sub

Public ReadOnly Property ParentName() As String
Get
Return myParent.Name
End Get
End Function

artifact said:
Can anyone tell me how to access a variable in a parent class from within a nested class?
Note: this snippet is only to illustrate my problem... I realize there's
no need to get at the parent name THROUGH the child object, but it quickly
shows what I'm after... any help would be appreciated. THANKS IN ADVANCE!
 
C

Chris Mullins

artifact said:
Can anyone tell me how to access a variable in a parent class from within
a nested class?

You should really read the sections in .NET on OO. Your termonolgy is off,
and that's going to cause you problems until you lear the right termonology.

A quick example of the type of code you're looking for:

Public MustInherit Class BaseClass
Public Function Multiply() As Integer
Return X * Y
End Function

Public MustOverride ReadOnly Property X() As Integer
Public MustOverride ReadOnly Property Y() As Integer
End Class

Public Class DerivedClass
Inherits BaseClass
Public Overrides ReadOnly Property X() As Integer
Get
Return 10
End Get
End Property

Public Overrides ReadOnly Property Y() As Integer
Get
Return 20
End Get
End Property
End Class
 
G

Guest

Thanks Marina... this is one approach I have taken... but I was thinking there was probably a more "automatic" way to do it... for example, controls in ASP.NET are "aware" of their containers by calling Me.Parent ... I thought there must be a way that object hierarchies would behave similarly. Maybe I'm looking for a magic trick that just isn't there

Thanks also to Chris... although I think I probably picked a bad example to illustrate what I want to do. What I was calling the "Child" class, would not be inheriting from the "Parent" class, as in your example. Maybe a better example would be if the "Parent" class were a book, and the "Child" class were a chapter... where a chapter wouldn't necessarily be inheriting from book, but rather would be a separate object that a book object would contain. I would want a chapter to innately "know" what book it was a child of. If you could tell me more specifically what I should read, I'd be all over it... When you referred to "sections in .Net on OO".. is there something in the SDK documentation I'm overlooking? Or did you mean more generally to seek out a .Net book covering OO topics... thanks again to both of you

-Scott
 
G

Guest

Actually, his terminlogy is fine. You simply did not understand the question. He asked about nested classes, not derived classes.
 
J

Jon Skeet [C# MVP]

artifact said:
Thanks Marina... this is one approach I have taken... but I was
thinking there was probably a more "automatic" way to do it... for
example, controls in ASP.NET are "aware" of their containers by
calling Me.Parent ... I thought there must be a way that object
hierarchies would behave similarly. Maybe I'm looking for a magic
trick that just isn't there?

You are - because an instance of a nested class doesn't have any
implicit instance of its containing class associated with it. Indeed,
there could be an instance of the nested class without any instances of
the container class ever being created.
 
C

Cor Ligthert

Hi Artifact,

You are looking to something as this?

Roughly done, do not look for proper datanames etc.

Cor

\\\
Private Sub Form1_Load(ByVal sender _
As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim my As New Parent
MessageBox.Show(my.Child)
End Sub
End Class
Public Class Parent
Private mParentName As String
Private mChild As ParentChild
Public ReadOnly Property Child() As String
Get
Return mChild.ParentName
End Get
End Property
Sub New()
mParentName = "parent name test"
mChild = New ParentChild
End Sub
Protected Friend Class ParentChild
Private Artifact As String
Public ReadOnly Property ParentName() As String
Get
Return Artifact
End Get
End Property
Friend Sub New()
MyBase.New()
Artifact = "I hope this helps?"
End Sub
End Class
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top