Reputation: 1899
So, this may be an issue of what should be child of what, but I am having difficulty figuring out the best way to perform the following.
Say I have a kitchen, and I want to issue a command to turn on my toaster, so I would call something like this in a button click event:
private void buttonToaster_On_Click(object sender, EventArgs e)
{
myKitchen.Toaster.Activate();
}
I envisioned doing what is depicted below. However, in C# it says that I have to make the RelayBoard "myStuff" a static. If for some reason I wanted to control multiple kitchens with this, I would prefer to not make this static, but how would I make it so I can instantiate a kitchen, have one relayboard that talks to everything in it, and create appliances under that kitchen that access the RelayBoard "myStuff" to write the command to?
class Kitchen
{
RelayBoard myStuff = new RelayBoard();
Appliance Toaster = new Appliance();
Appliance Coffee_Maker = new Appliance();
class Appliance
{
void Activate()
{
myStuff.WriteCommand(ApplianceAddress, 1);
}
void Deactivate()
{
myStuff.WriteCommand(ApplianceAddress, 0);
}
}
}
Upvotes: 0
Views: 932
Reputation: 10940
simply pass it a reference to the kitchen object:
class Appliance
{
private Kitchen _parent;
public Appliance(Kitchen parent) { _parent = parent; }
void Activate()
{
_parent.myStuff.WriteCommand(ApplianceAddress, 1);
}
void Deactivate()
{
_parent.myStuff.WriteCommand(ApplianceAddress, 0);
}
}
Upvotes: 2