472,330 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 2612
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.