Calling an object that contains an object

  • Thread starter Thread starter Jay Douglas
  • Start date Start date
J

Jay Douglas

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)?
 
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
 
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 said:
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:

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,
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
 
Jay Douglas said:
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.
 
Back
Top