Search code examples
c#functionclasscall

How to call the function of a sibling class, from a class within the other sibling class in C#


I have been assigned to upgrade a home media system by enabling remote control of the room audio system through the internet. As it is a legacy system, I am unable to alter much of the architecture. Below is a simplified form of the program. The system is composed of a few main classes initialized in the Home_Media_System object.

class Home_Media_System
{

    Network _networkdata

    Speakers _speakers

    Lights  _lights

    Household_devices household_devices

}

class Speakers
{
    void Play_Audio();
}


class Network
{
    UdpReceiver udpReceiver
    UdpTransmitter udpTransmitter

}


class UdpReceiver
{
    void receive_audio_player_command()
    {
        if(playCommand)
            start_audio();
    }

    void start_audio()
    {
        //How do I call the Play_Audio() function in class Speakers
    }

}

class UdpTransmitter
{
    void send_response();
}

My problem is to call the Play_Audio() function after receiving the command in the UdpReceiver class WITHIN the Network class. I hope this clears up the question title, as it was pretty hard to explain in a single sentence.

Note that the main classes are initialized in the Home_Media_System. Right now I am thinking of using double event handlers, but I wonder if there is a more elegant way to do so.


Solution

  • This is some bad hierarchy you have and there is something definitely wrong with design. But just to answer this we can use events/delegates in C# to communicate between siblings via parent. So I will define a event at two places:

    In your Network class and in UdpReceiver class and raise events when needed

    public class Network
    {
        private UdpReceiver udpReceiver;
        UdpTransmitter udpTransmitter
        public event EventHandler PlayAudioEvent;
    
        public void Network()
        {
            udpReceiver.PlayAudioEvent += PlayAudioEventHandler;
    
        }
    
        void PlayAudioEventHandler(object sender, EventArgs e)
        {
            if (PlayAudioEvent != null)
            {
                PlayAudioEvent(this, null);
            }
         }
    }
    
    public class UdpReceiver
    {
         public event EventHandler PlayAudioEvent;
        void receive_audio_player_command()
        {
            if(playCommand)
                start_audio();
        }
    
        void start_audio()
        {
            //How do I call the Play_Audio() function in class Speakers
            if (PlayAudioEvent != null)
            {
                PlayAudioEvent(this, null);
            }
        }
    
    }
    
    public class Home_Media_System
    {
    
    
            public void Home_Media_System()
            {
                _networkdata.PlayAudioEvent +=  PlayAudioEventHandler
            }
    
            void PlayAudioEventHandler(object sender, EventArgs e)
            {
                _speakers.PlayAudio();
            }
    

    also make all your classes as public/internal and method also as public.