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 10 19544
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
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
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
"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
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
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
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
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: LinuxGuy |
last post by:
Hi,
I have come across singleton class with some member variables are
declared as static with public scope.
As singleton class always return only one instance. ie.
single copy of object is ...
|
by: Bobby Howell |
last post by:
Hi.
I have the following which I use either on my server using cscript (.vbs
file) or identical code in my ASP pages. The problem is when running in ASP
I recieve the following error:...
|
by: Paul |
last post by:
Hi this is related to a previous post, hopefully just a bit clearer
description o the problem. I have a parent form that opens a new form (child
form) while still leaving the parent form open....
|
by: Woody Splawn |
last post by:
I am finding that time after time I have instances where I need to access
information in a variable that is public. At the same time, the books I
read say that one should not use public variables...
|
by: Chad |
last post by:
Hey everyone,
Ok, I am very security consious with my application(s). I am currently
working on an application in which I want to create several "global"
variables available ONLY to my...
|
by: Altman |
last post by:
I have a class that I want many others inherited off of. I have created a
variable in the parent class that I want the inherited classes to set. But
I do not want to set the variable in the...
|
by: zEE |
last post by:
How to close all the child windows when parent window get closed? in
ASP.net application
through scripting..
|
by: Richard K Miller |
last post by:
Here's an OOP question that perplexes me. It seems PHP doesn't treat
static variables correctly in child classes.
<?php
class ABC {
public $regular_variable = "Regular variable in ABC\n";...
|
by: agendum97 |
last post by:
On Jun 26, 4:53*pm, Gregor Kofler <use...@gregorkofler.atwrote:
If possible for your scenario, you could potentially use eval for
this. For example:
function MyClass(val)
{
var printIt =...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |