468,771 Members | 1,812 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

OOP question VB.NET

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
Jul 21 '05 #1
7 2529
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" <sc***@redflagmedia.com> wrote in message
news:A0**********************************@microsof t.com...
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

Jul 21 '05 #2
"artifact" <sc***@redflagmedia.com> wrote:
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

--
Chris Mullins
Jul 21 '05 #3
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
Jul 21 '05 #4
Actually, his terminlogy is fine. You simply did not understand the question. He asked about nested classes, not derived classes.
Jul 21 '05 #5
artifact <sc***@redflagmedia.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
termonology?
Jul 21 '05 #7
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
///
Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by William C. White | last post: by
2 posts views Thread by Albert Ahtenberg | last post: by
3 posts views Thread by James | last post: by
reply views Thread by Ollivier Robert | last post: by
1 post views Thread by Richard Galli | last post: by
4 posts views Thread by Albert Ahtenberg | last post: by
1 post views Thread by inderjit S Gabrie | last post: by
2 posts views Thread by Jack | last post: by
3 posts views Thread by Sandwick | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.