Changing parent properties

Y

Yuk Tang

Leading on from a recent topic, "How does child class access parent's
variables", I would like to ask, what's the most elegant way of
changing a parent/grandparent/great-grandparent's property?

The class Nest has a subclass SubNest. Within SubNest I want to work
with Title. I think if I can pass Title ByRef to the children and
grandchildren, it would work, but VB doesn't do so by default, and
indeed warns me against it: "Reference to a non-shared member
requires an object reference."



Public Class Nest
Private mstrTitle As String
Private mstrSubNests As SubNest
Public Property Title() As String
Public Property SubNests() As SubNest

Public Class SubNest
Private mstrNestBoolean As Boolean
Public Property NestBoolean() As Boolean
Public Sub Feck()
If NestBoolean = True Then
Title = "Hello"
End If
End Sub
End Class
End Class
 
Y

Yuk Tang

Yuk Tang said:
Leading on from a recent topic, "How does child class access
parent's variables", I would like to ask, what's the most elegant
way of changing a parent/grandparent/great-grandparent's property?

The class Nest has a subclass SubNest. Within SubNest I want to
work with Title. I think if I can pass Title ByRef to the
children and grandchildren, it would work, but VB doesn't do so by
default, and indeed warns me against it: "Reference to a
non-shared member requires an object reference."

I've done a modified version of this, with the parent having a
property that holds the child, and vice versa. So
Parent.Child.Parent,etc. I've also included a Title property in the
Parent to let me see what's going on.



Public Class Nest
Private mstrTitle As String
Private mstrSubNests As SubNest
Public Property Title() As String
Public Property SubNests() As SubNest

Public Class SubNest
Private mstrParent As Nest
Public Property Parent() As Nest
End Class

Public Sub New()
Dim sn As New SubNest
SubNests = sn
End Sub
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim n As New Nest
n.Title = "Hello"
n.SubNests.Parent = n
MsgBox("Original: " + n.Title)
MsgBox("Nested: " + n.SubNests.Parent.Title)
n.SubNests.Parent.Title = "There"
MsgBox("Original: " + n.Title)
MsgBox("Nested: " + n.SubNests.Parent.Title)
n.Title = n.SubNests.Parent.Title
MsgBox("Original: " + n.Title)
MsgBox("Nested: " + n.SubNests.Parent.Title)
End Sub



So this creates a Parent, sets its Title to "Hello", and sets
Child.Parent to Parent. So Parent.Title and Child.Parent.Title
should both be "Hello". This is indeed the case.

Hello, Hello

The next step sets Child.Parent.Title to "There", but leaves
Parent.Title alone. The expected result should therefore be "Hello"
and "There". However, this is what I get.

There, There

The last step should set Parent.Title to "There" as well, but it
seems that's already been done in step 2.

There, There

Doing further experiments, it looks as though setting one
automatically sets the other. Can anyone explain scopes or whatever
it is that's going on?
 

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

Similar Threads


Top