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

How do you reference an object trough its members?

P: n/a
Hi!
Consider the following:
Object A is a member of Object B.
Object A has a known identifier.
Object B has an unknown identifier.

How do we reference Object B using Object A?

I have tried using "owner", "container", "parent", etc. without success.
Please help.

It must be possible, there has to be a tie between these two objects, e.g.
you can reference Object A using Object B by qualifying, i.e. ObjectB.OBjectA.

Regards
M Shafaat
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
M Shafaat <MS******@discussions.microsoft.com> wrote:
Consider the following:
Object A is a member of Object B.
Object A has a known identifier.
Object B has an unknown identifier.

How do we reference Object B using Object A?
You can't. An object itself doesn't have any concept of an owner - the
same object could be referred to by several different variables in
different instances, or by none.
I have tried using "owner", "container", "parent", etc. without success.
Please help.

It must be possible, there has to be a tie between these two objects, e.g.
you can reference Object A using Object B by qualifying, i.e. ObjectB.OBjectA.


That's a one way navigation though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
If you want object B to be able to talk to A. That is easy. It is a member
of B; just use its name.

If you want object A to know it's "parent", you can set this relationship up
yourself. As an example, you might define the class A as

public class ObjectA {
public ObjectA(ObjectB parent) {
_parent = parent;
}

private ObjectB _parent;
}

Now, to create an A you will have to say,

ObjectB myB = new ObjectB();
ObjectA myA = new ObjectA(myB);

Alternatley, depending on the relationship you are trying to establish, you
might create method of B like:

public ObjectA CreateSubObjectA() {
return new ObjectB(this);
}

Then, you could use the code:

ObjectB myB = new ObjectB();
ObjectA myA = myB.CreateSubObjectA();

But, from a design persepctive here, please take care. Each class should not
know about the internals of the other class, and each class should have a
single responsibility. An example of this pattern is exemplified by controls
on a form. You can create a control and add it to the form with code similar
to:

Label label = new Label();
label.Name = "myLabel";
label.Text = "This is my label";
form1.Controls.Add(label);

Hope this helps...

Frisky

"M Shafaat" <MS******@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
Hi!
Consider the following:
Object A is a member of Object B.
Object A has a known identifier.
Object B has an unknown identifier.

How do we reference Object B using Object A?

I have tried using "owner", "container", "parent", etc. without success.
Please help.

It must be possible, there has to be a tie between these two objects, e.g.
you can reference Object A using Object B by qualifying, i.e.
ObjectB.OBjectA.

Regards
M Shafaat

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.