473,433 Members | 1,814 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,433 software developers and data experts.

How do you reference an object trough its members?

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
2 1249
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: klaas | last post by:
the following code gives rise to the beneath error message, only when a matrix object is instantiated as matrix<bool>, not with matrix<float>: /*returns a reference to the object at position...
2
by: RMD | last post by:
I need to be able to keep a list of object references, and null them out one by one at a later point in time. I realize this can be dangerous, but I have my reasons. I can't figure out, however,...
4
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though...
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
4
by: ffhansix | last post by:
Hi, I am trying to reference a COM component (.dll) from Visual Studio 2005 in my windows application in C# by adding a reference to a third party.dll file (tried .type library also), and i get...
1
by: Ivan | last post by:
If a reference to an object or an array is set to null, will the object/array and all of it’s members/elements be garbage collected as well (Assuming that no other references are held on the...
8
by: Michael Safyan | last post by:
Dear members of comp.lang.c++, I am a little bit confused about the differences between constant references and values. I understand that it is faster to use a constant reference ("const T&") than...
0
by: Abhishek Padmanabh | last post by:
I have been trying out boost's serialization library for the past few days. And I have come across a problem serializing a class that has a reference member. The code is posted as below: ...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
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
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
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...
1
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...
0
tracyyun
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...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.