can an object find its parent?

I

ilPostino

Hi,

This is a strange question. Firstly, I'm working with custom classes,
nothing available in .net and I _don't_ want to add a parent property.
Here is the problem.

Lets say I have two classes.

public class1
{
public class2 Class2Instance = new class2();
}

public class2
{
public void somemethod()
{
****LOGIC HERE****
}
}

When an instance of class1 is created, it creates an instance of class2
inside it. How can class2 get a reference to its parent? Without having to
define a parent property? The code that goes in LOGIC HERE needs to talk to
the holding class ....

thanks!

C
 
M

mdb

ilPostino said:
When an instance of class1 is created, it creates an instance of
class2 inside it. How can class2 get a reference to its parent?
Without having to define a parent property? The code that goes in
LOGIC HERE needs to talk to the holding class ....

Not that I know of...

Care to explain WHY you don't want a parent variable, so that we have some
understanding of your motivation for not wanting to do this??

-mdb
 
J

Jay B. Harlow [MVP - Outlook]

ilPostino,
I can think of 3 options:

1. Define a parent property.
2. Pass parent as a parameter to somemethod
3. Define class1 to be a singleton.

Most of the time I would find 1 or 2 to be the most appropriate.

Hope this helps
Jay
 
P

Paul E Collins

I don't think what you describe is possible. There might be a
convoluted way to do it using reflection, but it seems quite unlikely.

Why are you so unwilling to store a reference to the parent in the
child?

P.
 
T

typeMismatch

I didn't want a parent because I'm lazy and I'd have to pass that when
creating class2's ....

It may sound lame except I have an object structure nearing hundreds of
related objects. Having to always pass around parent information is a pain
but .... as I guessed I have no choice so have started work adding parent
properties...

thanks

-c
 

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