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

How does child class access parent's variables

P: n/a
Hi all,
Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

'*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
'*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic
Apr 6 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Goran Djuranovic wrote:
Hi all,
Does anyone know how to declare a variable in a class to be accessible
ONLY from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

'*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
'*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic


Variables declared as Private are just that, private.
Variables declared as Friend can be referenced by a sub class.
Variables declared as Public are available to everyone.

Unless Child is a sub-class of Parent, the Friend variables are not
available.

One thing I could suggest, if the Child really needs to see the Private
or Friend variables of the Parent, keep them in a Private collection,
with the key value being the name of the variable, and pass that
collection byRef to the constructor of the Child class, which can then
hold a reference to the collection.

Tom
Apr 6 '06 #2

P: n/a
Hello, Goran,

"Private" variables are only accessible to the class. "Protected"
variables are accessible to the class and to any derived (i.e.
Inherited) classes.

Cheers,
Randy
Goran Djuranovic wrote:
Hi all,
Does anyone know how to declare a variable in a class to be accessible
ONLY from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

'*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
'*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic

Apr 6 '06 #3

P: n/a
You've got a number of problems here.

1. In the constructor of Parent you create an instance of a Child object and
it
immediately goes out of scope.

2. Every time you create an instance of a parent object, you automatically
create
an instance of a Child object. This never takes care of the situation
where
the parent has no children.

If you rewrite it thus, then you will be able to achieve what you are asking
for:

Public Class Parent

Private m_age As String
Private m_children As ArrayList

Public Sub New()

m_age = String.Empty

m_children = New ArrayList

End Sub

Public Sub New(age As String)

m_age = age

End Sub

Public Property Age() As String

Get
Return m_age
End Get

Set(value As String)
m_age = value
End Set

End Property

Public Sub AddChild()

m_children.Add(New Child(Me))

End Sub

Public ReadOnly Property Child(index As Integer) As Child

Get
Return CType(m_children(index), Child)
End Get

End Property

End Class

Public Class Child

Private m_parent as Parent

Public Sub New(parent As Parent)

m_parent = parent

End Sub

Public ReadOnly Property ParentAge() As String

Get
Return m_parent.Age
End Get

End Property

End Class

Then you can use the objects thus:

Dim _parent As New Parent

_parent.Age = "1/1/2000"

_parent.AddChild()

Console.WriteLine(_parent.Child(0).ParentAge)

or:

Dim _parent As New Parent("1/1/2000")

_parent.AddChild()

Console.WriteLine(_parent.Child(0).ParentAge)
"Goran Djuranovic" <go**************@newsgroups.nospam> wrote in message
news:OL**************@TK2MSFTNGP04.phx.gbl...
Hi all,
Does anyone know how to declare a variable in a class to be accessible ONLY
from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

'*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
'*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic
Apr 7 '06 #4

P: n/a
"ByRef" was exactly what I used. It works like a charm. But thanks for your
response, anyway.

Goran Djuranovic

"tomb" <to**@technetcenter.com> wrote in message
news:zF*************@bignews8.bellsouth.net...
Goran Djuranovic wrote:
Hi all,
Does anyone know how to declare a variable in a class to be accessible
ONLY from a classes instantiated within that class?
For example:
************* CODE *****************
Public Class Parent
'*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"
Public Sub New ()
Dim child As New Child()
End Sub
End Class
Public Class Child
Public Sub GetParentAge()
'*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age) End Sub
End Class
*****************************************
TIA
Goran Djuranovic


Variables declared as Private are just that, private.
Variables declared as Friend can be referenced by a sub class.
Variables declared as Public are available to everyone.

Unless Child is a sub-class of Parent, the Friend variables are not
available.

One thing I could suggest, if the Child really needs to see the Private or
Friend variables of the Parent, keep them in a Private collection, with
the key value being the name of the variable, and pass that collection
byRef to the constructor of the Child class, which can then hold a
reference to the collection.

Tom

Apr 11 '06 #5

P: n/a
Hi Stephany,
Thanks for your response. I don't want to get into 1. & 2. discussion,
because I just wrote the code so people can understand what I want, easily.

From the code you sent, it looks like every child will create a new instance
of its parent, which hold the refs for all the children created up to that
point, no? Seems like a waste of memory, because I want to access ONLY ONE
property of a parent.

Also, I might be wrong, but in your code you are passing a parent parameter
by value, a not reference, which means if that ONE property changes for the
parent, I am not going to be able to see it in my child object, no?

Anyway, I decided to go with ByRef parameter for the child object
constructor. Do you see any problems with that?

I appreciate your response.
Goran Djuranovic
"Stephany Young" <noone@localhost> wrote in message
news:uw**************@TK2MSFTNGP04.phx.gbl...
You've got a number of problems here.

1. In the constructor of Parent you create an instance of a Child object
and it
immediately goes out of scope.

2. Every time you create an instance of a parent object, you automatically
create
an instance of a Child object. This never takes care of the situation
where
the parent has no children.

If you rewrite it thus, then you will be able to achieve what you are
asking for:

Public Class Parent

Private m_age As String
Private m_children As ArrayList

Public Sub New()

m_age = String.Empty

m_children = New ArrayList

End Sub

Public Sub New(age As String)

m_age = age

End Sub

Public Property Age() As String

Get
Return m_age
End Get

Set(value As String)
m_age = value
End Set

End Property

Public Sub AddChild()

m_children.Add(New Child(Me))

End Sub

Public ReadOnly Property Child(index As Integer) As Child

Get
Return CType(m_children(index), Child)
End Get

End Property

End Class

Public Class Child

Private m_parent as Parent

Public Sub New(parent As Parent)

m_parent = parent

End Sub

Public ReadOnly Property ParentAge() As String

Get
Return m_parent.Age
End Get

End Property

End Class

Then you can use the objects thus:

Dim _parent As New Parent

_parent.Age = "1/1/2000"

_parent.AddChild()

Console.WriteLine(_parent.Child(0).ParentAge)

or:

Dim _parent As New Parent("1/1/2000")

_parent.AddChild()

Console.WriteLine(_parent.Child(0).ParentAge)
"Goran Djuranovic" <go**************@newsgroups.nospam> wrote in message
news:OL**************@TK2MSFTNGP04.phx.gbl...
Hi all,
Does anyone know how to declare a variable in a class to be accessible
ONLY from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

'*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
'*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic

Apr 11 '06 #6

P: n/a
Hi Randy,
Can't use "Protected" because a child class is not derived. I decided to go
with ByRef parameter for child constructor.

Thanks for your response
Goran Djuranovic
"R. MacDonald" <sc****@NO-SP-AMcips.ca> wrote in message
news:44***********************@news.wanadoo.nl...
Hello, Goran,

"Private" variables are only accessible to the class. "Protected"
variables are accessible to the class and to any derived (i.e. Inherited)
classes.

Cheers,
Randy
Goran Djuranovic wrote:
Hi all,
Does anyone know how to declare a variable in a class to be accessible
ONLY from a classes instantiated within that class?
For example:
************* CODE *****************
Public Class Parent
'*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"
Public Sub New ()
Dim child As New Child()
End Sub
End Class
Public Class Child
Public Sub GetParentAge()
'*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age) End Sub
End Class
*****************************************
TIA
Goran Djuranovic

Apr 11 '06 #7

P: n/a
Goran,

Stephany has showed you the standard solution for your problem and AFAIK
the only right possible one (although you can as well use collection base
instead of arraylist or any other collection, but that is a detail, she
wanted to show it you probably as short as possible).

Why don't you accept it? It is so stupid to have to read that you want to
save memory and than tells that you use the by ref, which uses forever more
than the by value. Although this is absolute not related to your question
and even not to mention because it are AFAIK only 8 bytes. With that for me
and probably for most showing your knowledge, don't try to hide that, we all
had to start once.

Just my 2 eurocents.

Cr
Apr 11 '06 #8

P: n/a
Goran Djuranovic wrote:
Hi Stephany,
Thanks for your response. I don't want to get into 1. & 2. discussion,
because I just wrote the code so people can understand what I want, easily.

From the code you sent, it looks like every child will create a new instance
of its parent, which hold the refs for all the children created up to that
point, no? Seems like a waste of memory, because I want to access ONLY ONE
property of a parent.

Only the New keyword can create object instances. Each child will have
a reference to it's parent, but it will not actually create a new
instance of the parent. There is very little memory usage associated
with using this technique.
Also, I might be wrong, but in your code you are passing a parent parameter
by value, a not reference, which means if that ONE property changes for the
parent, I am not going to be able to see it in my child object, no?

You will see the change. Since the variable is passed by value that
means a new reference is created on the stack. However, that new
reference just happens to point to the same parent object. Changes to
the parent will be seen from within the child. I know...the ByVal and
ByRef keywords are confusing.
Anyway, I decided to go with ByRef parameter for the child object
constructor. Do you see any problems with that?
Yes. When passing ByRef you are allowing the possibility that code
could change the caller's reference variables and what they are
actually referencing. It's rare that you would need to pass an object
reference ByRef. Let me explain it another way. ByVal and ByRef do
not determine whether the object is a reference type or a value type.
They control how variables are passed to functions.

I appreciate your response.
Goran Djuranovic


Apr 11 '06 #9

P: n/a
Read inline...

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Goran,

Stephany has showed you the standard solution for your problem and AFAIK
the only right possible one (although you can as well use collection base
instead of arraylist or any other collection, but that is a detail, she
wanted to show it you probably as short as possible).
You know not far, and please take that [MVP] out of your name
representation. People may think you are a Most Valuable Programmer.
Why don't you accept it?
Do I have to?
It is so stupid to have to read that you want to save memory and than
tells that you use the by ref, which uses forever more than the by value.
Ouhh. I am sorry. Some people were not born as MVPs.
Anyway, ByVal & ByRef are VERY confusing keywords. There are hundreds of
discussions on the web about them, and people still can't have it
straightened out. Logically, one would think the ByVal passes a new copy of
a variable, and ByRef passes a pointer. A copy of an object (rather than a
pointer) WOULD mean more memory requirements. Not in VB.NET's case.
Although this is absolute not related to your question and even not to
mention because it are AFAIK only 8 bytes.
Again, you know not far. It is actually 4 bytes. You are embarrasing the
real MVPs. :-)
With that for me and probably for most showing your knowledge, don't try
to hide that, we all had to start once.
If you don't have anything better to say, at least don't insult people in
this group. Look at Brian's response for a "responding template".
Just my 2 eurocents.


And last, keep your stinking 2 cents for yourself. I will survive without
them.

Cheers

Apr 12 '06 #10

P: n/a
Thanks Brian. Your explanation was very helpfull. I was very suprised by how
ByVal and ByRef behave. But, it looks like behavior is different for .NET
v1.0 & .NET v1.1.

From DotNetExtreme.com:
"Passing a parameter by Reference means that if changes are made to the
value of a variable passed, these changes are reflected back in the calling
routine.
Objects are Reference type data. They maintain a reference to where the
actual data is stored on the stack. Normally what occurs when an object is
passed by reference is that this memory pointer from the stack is passed to
the function or procedure. As a result of only passing a pointer to the
referenced data, the function or procedure has access to the original data.

However, VB.NET does not follow this model. When an object's property is
passed by reference to a function or procedure, the data is copied. Not only
is the data copied, but since by definition variables passed by reference
allow for the return of changes, the data is copied back. Thus, while most
implementation avoid copying the data when a variable is passed by
reference, VB.NET actually passes it twice -- a large performance hit on a
function call that passes data by reference."

The problem here is that they don't say this is valid for .NET v1.0, ONLY.

In .NET v1.1, there is no copying, and overhead.
Follow the links:
http://www.codeproject.com/useritems/VBnet_Methods.asp
http://msdn.microsoft.com/library/de...pec7_1_6_2.asp

Thanks everyone who responded: tomb, R. MacDonald, Steph, and Brian.
Consider this thread CLOSED.

Goran Djuranovic

"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Goran Djuranovic wrote:
Hi Stephany,
Thanks for your response. I don't want to get into 1. & 2. discussion,
because I just wrote the code so people can understand what I want,
easily.

From the code you sent, it looks like every child will create a new
instance
of its parent, which hold the refs for all the children created up to
that
point, no? Seems like a waste of memory, because I want to access ONLY
ONE
property of a parent.


Only the New keyword can create object instances. Each child will have
a reference to it's parent, but it will not actually create a new
instance of the parent. There is very little memory usage associated
with using this technique.
Also, I might be wrong, but in your code you are passing a parent
parameter
by value, a not reference, which means if that ONE property changes for
the
parent, I am not going to be able to see it in my child object, no?


You will see the change. Since the variable is passed by value that
means a new reference is created on the stack. However, that new
reference just happens to point to the same parent object. Changes to
the parent will be seen from within the child. I know...the ByVal and
ByRef keywords are confusing.
Anyway, I decided to go with ByRef parameter for the child object
constructor. Do you see any problems with that?


Yes. When passing ByRef you are allowing the possibility that code
could change the caller's reference variables and what they are
actually referencing. It's rare that you would need to pass an object
reference ByRef. Let me explain it another way. ByVal and ByRef do
not determine whether the object is a reference type or a value type.
They control how variables are passed to functions.

I appreciate your response.
Goran Djuranovic

Apr 12 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.