Consider the following interfaces:
public interface IComponent { } public interface ISwitch : IComponent { bool IsOn { get; } event EventHandler SwitchedOff; event EventHandler SwitchedOn; } public interface ISwitchable : ISwitch, IComponent { void SwitchOff(); void SwitchOn(); } public interface IPowerSwitch : ISwitchable, ISwitch, IComponent { } public interface IHeatingElement : ISwitchable, ISwitch, IComponent { }
I implemented IPowerSwitch in the class as follows:
public class Kettle : IPowerSwitch { event EventHandler PowerOnEvent; event EventHandler PowerOffEvent; object objectLock = new Object(); public bool IsPowerOn; public Kettle() { IPowerSwitch p = (IPowerSwitch)this; p.SwitchedOn += new EventHandler(On_PowerOn_Press); p.SwitchedOff += new EventHandler(On_PowerOff_Press); } void ISwitchable.SwitchOff() { EventHandler handler = PowerOffEvent; if (handler != null) { handler(this, new EventArgs()); } } void ISwitchable.SwitchOn() { EventHandler handler = PowerOnEvent; if (handler != null) { handler(this, new EventArgs()); } } bool ISwitch.IsOn { get { return IsPowerOn ; } } event EventHandler ISwitch.SwitchedOff { add { lock (objectLock) { PowerOffEvent += value; } } remove { lock (objectLock) { PowerOffEvent -= value; } } } event EventHandler ISwitch.SwitchedOn { add { lock (objectLock) { PowerOnEvent += value; } } remove { lock (objectLock) { PowerOnEvent -= value; } } } protected void On_PowerOn_Press(object sender, EventArgs e) { if (!((IPowerSwitch)sender).IsOn) { Console.WriteLine("Power Is ON"); ((Kettle)sender).IsPowerOn = true; ((IPowerLamp)this).SwitchOn(); } else { Console.WriteLine("Already ON"); } } protected void On_PowerOff_Press(object sender, EventArgs e) { if (((IPowerSwitch)sender).IsOn) { Console.WriteLine("Power Is OFF"); ((Kettle)sender).IsPowerOn = false; ((IPowerLamp)this).SwitchOff(); } else { Console.WriteLine("Already OFF"); } } }
Now I want to implement the IHeatingElement interface in this class. IHeatingElement has the same methods as IPowerSwitch. So how can I implement SwitchOn and SwitchOff from IHeatingElement. If I try to implement something like IPowerSwitch.SwitchOff (), I get an error
'IPowerSwitch.SwitchOff' in the explicit interface declaration is not a member of the interface.
What I want to do is that when the power is turned on at the event, the heat after that should be raised. And when the heating is off, turn off the power off event.
This is my first question here, so please guide me if something is wrong with the question. Thank you in advance for your help.
source share