473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

accessing parent variables

how can i access variables defined in the parent class from the child class:
By some way other than passing as parameter to constructor.

I mean.

class Parent{

public int b;

public Method()
{
b = 5;
Child child = new Child();
}

}

class Child{

public Method()
{
int a = b; // to do this..
}
}

Nov 15 '05 #1
2 11578
In your case, you have a HAS-A relationship (and not a parent-child or IS-A
relationship). The only way to have the child making access to public
members of your "Parent" class is either to pass the member(s) in the
ctor/methods or better to pass the "this" of Parent in the ctor of your
"Child"

José
"Rustam Asgarov" <ru****@boun.edu.tr> a écrit dans le message de
news:Or**************@TK2MSFTNGP11.phx.gbl...
how can i access variables defined in the parent class from the child class: By some way other than passing as parameter to constructor.

I mean.

class Parent{

public int b;

public Method()
{
b = 5;
Child child = new Child();
}

}

class Child{

public Method()
{
int a = b; // to do this..
}
}

Nov 15 '05 #2

"Rustam Asgarov" <ru****@boun.edu.tr> wrote in message
news:Or**************@TK2MSFTNGP11.phx.gbl...
how can i access variables defined in the parent class from the child class: By some way other than passing as parameter to constructor.

I mean.

class Parent{

public int b;

public Method()
{
b = 5;
Child child = new Child();
}

}

class Child{

public Method()
{
int a = b; // to do this..
}
}


I don't think it is possible. What if I defined:

class OtherParent
{
private Child _myChild;

public OtherParent(Child theChild)
{
_myChild = theChild;
}
}

Which "parent" should be used now?

By the way: "child classes" usually refers to classes that are defined (as
opposed to "used")
inside some parent class. Like:

class Parent
{
class Child
{ ... }

...
}

In this case "Child" CAN refer to fields (even private!) of "Parent".
Hans Kesting

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jiri Brada | last post by:
Hi, I have got a following problem with using Javascript: I have a HTML page with pictures' thumbnails. After clicking on any thumbnail, I would like to open a new window with the original...
3
by: Christopher Benson-Manica | last post by:
I have an HTMLInputElement in a <td>. I want to access all the <td>'s in the same row as the input element. var tr=theInputElement.parentNode.parentNode; Why isn't the input element's...
5
by: Trey | last post by:
Is it possible to access the parent from an instance of a "child" class? For example in the code below I would like to be able to reference which exact army a troop belonged to by doing something...
3
by: Ladislav Soukup | last post by:
Hi, I'm now migrating to C# and .NET 1.1 programming from PHP and PERL and I have one problem and I cannot solve it (google didn't help). I have two forms - FORM1 and FORM2 FORM1 will make...
5
by: TS | last post by:
is it preferred to access member variables directly in code, on the page that declared them, versus going thru a property accessor? I would think that since theres no security concerns or anything...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
0
by: ppotter10 | last post by:
I'm working on an app which uses a parent form and child forms in C++.Net. I want to access a static arraylist variable of the parent form from a child form. In order to do so, I wanted to use the...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.