Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Declare a new delegate type called BoolToggle Returns void Takes in a bool Cr

ID: 3828522 • Letter: C

Question

C#

Declare a new delegate type called BoolToggle

Returns void

Takes in a bool

Create a LightSwitch class that does the following:

Declares an event of type BoolToggle called OnToggle

Has a public string property called Name

Has one constructor that takes in a single string name parameter

Has a bool property called IsOn

i.Public get, private set

ii.Defaults to false

Has a Toggle() method that simply flips the value of the IsOn bool, then notifies all subscribers by firing the OnToggle event.

Override the ToString() to return a simple String with the name and current state of IsOn.

Create a Lightbulb class that does the following:

Has a public string property called Name

Has a constructor that takes in a single string name parameter

Has a ConnectSwitch method that takes in a LightSwitch instance and, if not already connected, subscribes to that switch’s OnToggle event.

Has a DisconnectSwitch method that takes in a LightSwitch instance and, if connected, unsubscribes from that switch’s OnToggle event.

If you haven’t guessed yet, the Lightbulb will need to keep track of the LightSwitches to which it is connected. A Lightbulb may only be connected to at most 2 separate LightSwitches. A Lightbulb can never be connected to a specific LightSwitch more than once.

The Lightbulb is on IF AND ONLY IF:

i.It is connected to one LightSwitch and the LightSwitch is on.

ii.It is connected to two LightSwitches where both LightSwitches have the same IsOn value.

iii.All other cases mean the Lightbulb is off.

The Lightbulb is allowed to check a switch’s IsOn status IF AND ONLY IF it is done during the connect/disconnect methods. AT NO OTHER TIME is the bulb allowed to check the switch’s IsOn state. Instead, the bulb’s IsOn state should only be impacted by the firing of OnToggle events.

Override the ToString() to return a simple String with the name and current state of the Lightbulb. It should also list the switches to which it is connected and their respective IsOn states. If a bulb is not connected to any switch, some simple indicator of that is fine. In all cases, make sure the bulb’s string is readable and user friendly.

Now, create an application that allows the user to:

Create and name new LightSwitches

Create and name new Lightbulbs

Connect/disconnect Lightbulbs and LightSwitches

Toggle specific LightSwitches

Print out the current state of all LightSwitches and Lightbulbs

Quit the application

The main menu should loop as needed until the user decides to exit the app

s. De, ares ar'eent of typeBourogJe caleOn Tuuele Has crearistructur t"la: tKesria Sriadestrrig rern.peranne· c Has aToggle(ime hac thetalmp?filasticvalucor thelsCnbael then normes al sussalacieby fi 1ngthaCr Toggc cet. 4) Now, crestc anappitation th tallows the use-to: Them anmaru shouldl3cpaerecedinal hauardstices to cit tep.

Explanation / Answer

You create delegates for properties with Delegate.CreateDelegate; this can be open for any instance of the type, of fixed for a single instance - and can be for getter or setter; I'll give an example in C#...