473,602 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1259
M Shafaat <MS******@discu ssions.microsof t.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.co m>
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 CreateSubObject A() {
return new ObjectB(this);
}

Then, you could use the code:

ObjectB myB = new ObjectB();
ObjectA myA = myB.CreateSubOb jectA();

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******@discu ssions.microsof t.com> wrote in message
news:7F******** *************** ***********@mic rosoft.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
4135
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 (Row,Col) in matrix*/ template <class num_type,template <class T> class functor> num_type & matrix<num_type,functor >::operator()(const int Row,const int Col) {if (Row<rows && Col<cols) {vector<num_type> & x=matrix_core; //num_type & y=x;<-is also...
2
1705
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, how to keep what is essentially a reference to a reference. I want to store my objects in an ArrayList, then loop through the array list and null out the original reference I was given when I added it to my Array List. Below is some example...
4
2536
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 my recordset did not. i read from pasts posts that some references will not convert. so i tried to change the dao 3.51 reference to dao 3.6 in access 97. then i imported all of the objects into an access 2003. it did not work. so my second...
7
5752
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: Fruit lemon = new Fruit(); Product prod = (Product)lemon; // Now prod is a Fruit Type!! I want to get a Product reference from a Fruit object, is it possible? thank you.
4
10825
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 this error from the compiler when trying to compile the solution. Does anyone know why this occurs, and how it can be resloved, i would be very grateful indeed.
1
1370
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 members/elements, and none of these members/elements hold references to unmanaged resources.)? For example: public class A { public A(){}
8
4088
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 a value ("T") since the former does not require copying whereas the latter does, is that correct? Also, I un derstand that "const T&" allows for polymorphism whereas "T" will generate code cuttting. On the former points, I am fairly confident......
0
2097
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: #include <iostream> #include <fstream> #include <string> #include <boost/archive/xml_iarchive.hpp>
7
2823
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 to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
0
7993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6730
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.