By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

Changing parent properties

P: n/a
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
--
Cheers, ymt.
Apr 8 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Yuk Tang <ji********@yahoo.com> wrote in
news:Xn*******************************@130.133.1.4 :

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?
--
Cheers, ymt.
Apr 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.