How does child class access parent's variables

  • Thread starter Goran Djuranovic
  • Start date
G

Goran Djuranovic

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
 
T

tomb

Goran said:
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
 
R

R. MacDonald

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
 
S

Stephany Young

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)


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
 
G

Goran Djuranovic

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

Goran Djuranovic
 
G

Goran Djuranovic

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
 
G

Goran Djuranovic

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
 
C

Cor Ligthert [MVP]

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
 
B

Brian Gideon

Goran said:
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.
 
G

Goran Djuranovic

Read inline...

Cor Ligthert said:
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
 
G

Goran Djuranovic

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/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec7_1_6_2.asp

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

Goran Djuranovic
 

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

Top