473,396 Members | 2,098 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,396 software developers and data experts.

Calling an object that contains an object

I'm not to sure how to phrase this question. I need to know how to call a
property of an object that contains an object. I'll use PetHouse and Cat
for an example. (I didn't use get or set to try to keep it simple and
short)

class CatHouse {
public Cat TheCat;
public bool HasCarpet;
}

class Cat {
public string CatName;
}

What I need to know is: From the TheCat object, how do I retrieve the value
of a property in CatHouse (i.e. CatHouse.HasCarpet)?

--
Jay Douglas
Fort Collins, Colorado
http://www.jaydouglas.com

Nov 16 '05 #1
5 1303
Let me make sure I understand you:
You have an object. You want to find the object that refers to it as a
property, so that you can discover the value of ANOTHER property of the
containing object. Is that right?

Answer: you have a design issue.
If it is important for a cat to know what cat house it is inside, it should
have a reference to the cat house. Otherwise, the house that the cat is in
is not a property of the cat, and is not discoverable from it.

Hope this helps,
--- Nick

"Jay Douglas" <ja****************@squarei.com> wrote in message
news:eU**************@tk2msftngp13.phx.gbl...
I'm not to sure how to phrase this question. I need to know how to call a
property of an object that contains an object. I'll use PetHouse and Cat
for an example. (I didn't use get or set to try to keep it simple and
short)

class CatHouse {
public Cat TheCat;
public bool HasCarpet;
}

class Cat {
public string CatName;
}

What I need to know is: From the TheCat object, how do I retrieve the value of a property in CatHouse (i.e. CatHouse.HasCarpet)?

--
Jay Douglas
Fort Collins, Colorado
http://www.jaydouglas.com

Nov 16 '05 #2
So, the solution is to have a property of Cat that has a property of
TheHouse such as Cat.ContainingCatHouse .. So when generating
CatHouse.TheCat in the class CatHouse I would have to set a by reference
value such as TheCat.ContainingCatHouse = this .. To recreate the classes:

class CatHouse {
public Cat TheCat;
public bool HasCarpet;

private void createTheCat() {
TheCat = new Cat();
TheCat.ContainingCatHouse = this;
}
}

class Cat {
public string CatName;
public CatHouse ContainingCatHouse;

private bool checkForCarpetOnCatHouse() {
return this.ContainingCatHouse.HasCarpet;
}
}

This sounds a little redundant and can create a little nightmare, but if
this is the solution, I can work with it.

--
Jay Douglas
Fort Collins, Colorado
http://www.jaydouglas.com
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:KbEPc.69563$8_6.418@attbi_s04...
Let me make sure I understand you:
You have an object. You want to find the object that refers to it as a
property, so that you can discover the value of ANOTHER property of the
containing object. Is that right?

Answer: you have a design issue.
If it is important for a cat to know what cat house it is inside, it should have a reference to the cat house. Otherwise, the house that the cat is in is not a property of the cat, and is not discoverable from it.

Hope this helps,
--- Nick

"Jay Douglas" <ja****************@squarei.com> wrote in message
news:eU**************@tk2msftngp13.phx.gbl...
I'm not to sure how to phrase this question. I need to know how to call a property of an object that contains an object. I'll use PetHouse and Cat for an example. (I didn't use get or set to try to keep it simple and
short)

class CatHouse {
public Cat TheCat;
public bool HasCarpet;
}

class Cat {
public string CatName;
}

What I need to know is: From the TheCat object, how do I retrieve the

value
of a property in CatHouse (i.e. CatHouse.HasCarpet)?

--
Jay Douglas
Fort Collins, Colorado
http://www.jaydouglas.com


Nov 16 '05 #3
Jay Douglas <ja****************@squarei.com> wrote:
So, the solution is to have a property of Cat that has a property of
TheHouse such as Cat.ContainingCatHouse .. So when generating
CatHouse.TheCat in the class CatHouse I would have to set a by reference
value such as TheCat.ContainingCatHouse = this .. To recreate the classes:
<snip>
This sounds a little redundant and can create a little nightmare, but
if this is the solution, I can work with it.


In what way is it redundant? In the code in your first post, there was
no guarantee that a cat would be in *any* CatHouse or only *one*
CatHouse, so how could you possibly know whether "the" CatHouse
containing it had carpet?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon,
So assuming that a Cat is always be in a CatHouse I would always like to
be able to reference the CatHouse that the Cat is in. To I have to keep
changing the value of Cat.ContainingCatHouse each time Cat changes a
CatHouse?

--
Jay Douglas
Fort Collins, Colorado
http://www.jaydouglas.com
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jay Douglas <ja****************@squarei.com> wrote:
So, the solution is to have a property of Cat that has a property of
TheHouse such as Cat.ContainingCatHouse .. So when generating
CatHouse.TheCat in the class CatHouse I would have to set a by reference
value such as TheCat.ContainingCatHouse = this .. To recreate the
classes:
<snip>
This sounds a little redundant and can create a little nightmare, but
if this is the solution, I can work with it.


In what way is it redundant? In the code in your first post, there was
no guarantee that a cat would be in *any* CatHouse or only *one*
CatHouse, so how could you possibly know whether "the" CatHouse
containing it had carpet?

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

Nov 16 '05 #5
Jay Douglas <ja****************@squarei.com> wrote:
So assuming that a Cat is always be in a CatHouse I would always like
to be able to reference the CatHouse that the Cat is in. To I have to
keep changing the value of Cat.ContainingCatHouse each time Cat
changes a CatHouse?


Yes - otherwise how would the framework know what you'd done? Just
because *you* know that you're making sure that you're moving it rather
than putting it in another CatHouse as well doesn't mean that the
framework knows it.

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

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

Similar topics

11
by: Midas | last post by:
In my "main" code, I would like to create two new objects (ObjectA and ObjectB) and have ObjectA directly call an ObjectB method. In order to do this I believe ObjectA must hold a reference to...
4
by: Bob Murdoch | last post by:
I have an ASP application that calls a COM function to create a custom report as an Excel file. This works in a synchronous fashion, as long as the report does not take too long to create. If...
4
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
3
by: Jerome Cohen | last post by:
AI am trying to call a third-party web service. this service expects an XML fragment that contains the request plus other parameter. adding the web reference created the syntax below(reference.vb)....
20
by: lars.uffmann | last post by:
I have to work on a rather big project that a bunch of people wrote and that has no useful documentation at all, my C++ has rusted in a bit, now I stumbled over a constructor that looks something...
1
by: David R | last post by:
I have a Net 2.0 client (C# Winform) calling an Axis web service. I can authenticate, and send a request that is accepted and responded to, but I cannot see the response. The return for the...
10
by: Max Yuzhakov | last post by:
Hello! It is correct behaviour for python to call __del__ on some identity of a class object more than once? In brief I shall describe a situation. Sorry for my english. For debugin...
1
by: amgupta8 | last post by:
Note: This problem occurred when I updated the JDK from 1.3.1 to 1.4.1 or 1.4.2. Nothing else was changed in the code, other than updating the JDK on the database server (dbm cfg parm jdk_path) and...
2
by: rocketfire97 | last post by:
I'm trying to call a COM object using C# but having no luck getting values back for passed in ref objects. I've tried the same call using VB.NET and can get data back. How would I implement the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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,...

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.