Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:44:29+00:00 2026-06-03T23:44:29+00:00

I have an interface called IDataIO: public interface IDataIO { event DataReceivedEvent DataReceived; //…..more

  • 0

I have an interface called IDataIO:

public interface IDataIO
{
  event DataReceivedEvent DataReceived;
  //.....more events,methods and properties
}

I also have multiple classes that implement this interface, namely UdpIO, TcpIO, SerialIO.

Now, I have an IO class that allows me to switch between different input/output hardware. Each instance of this class has a CurrentIODevice property, which could be one of SerialIO,UdpIO or TcpIO. When this property is assigned, i attach 1 or more handlers to the DataReceivedEvent so that my GUI is notified when incoming data is received, as well as other classes that need to be notified.

public class IO
{
  IDataIO CurrentIODevice;

  public IO()
  {
    SerialIO serial = new SerialIO();
    TcpIO tcp = new TcpIO();
    UdpIO udp = new UdpIO();
    CurrentIODevice = serial;
  }
}

I also have a IOManager class that holds multiple IO objects.

public class IOManager
{
  List<IO> Ports = new List<IO>();
  public IOManager()
  {
    Ports.Add(new IO());
    Ports.Add(new IO());
  }

  Ports[0].CurrentIODevice = serial;
  Ports[0].CurrentIODevice.DataReceivedHandler += MyGuiUpdate;
  Ports[0].CurrentIODevice.DataReceivedHandler += MyDataProcessing;
}

My concern (its not an issue atm) is how I am going to change between different IDataIO interfaces at runtime.

What is the effect of, at runtime, performing the following statement:

//i know this is illegal but just to demonstrate
IOManager.Ports[0].CurrentIODevice = tcp; 

Will the event handlers still be functioning (and correctly)?

Do i need to unassign the events before the CurrentIODevice is assigned, and then re-assign the handlers again after? If this is the case, I can see this approach getting quite messy, so if anyone has a better approach to this problem I’m all ears 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:44:30+00:00Added an answer on June 3, 2026 at 11:44 pm

    No, your handlers will not work because they’re attached to the old object. Interfaces provides…an interface to an object, see it as a kind of contract but they’re not a different object themselves.

    If you need to switch between different implementations of the interface (at run-time) and to keep all handlers working you have to have the same object reference for the interface itself, kind of strategy pattern (more or less).

    In your case you may, for example, implement the IDataIO interface in a DataIO object. It’ll expose a property (or a method, I think its intent is more clear) to switch between different implementations of that interface (serial, TCP or whatever). It’ll be the only one object to attach an event handler to that interface (and it’ll drop the handler when the concrete implementation will change). Users of that object will always see it, whatever it’s the concrete implementation it’s using.

    Example

    This is a small example to explain this concept. The generic interface is this:

    interface IDataIO
    {
        void Write(byte[] data);
    
        byte[] Read();
    
        event EventHandler DataReceived;
    }
    

    This is the concrete implementation of IDataIO, other classes will use only this class directly:

    sealed class DataIO : IDataIO
    {
        public void SetChannel(IDataIO concreteChannel)
        {
            if (_concreteChannel != null)
                _concreteChannel.DataReceived -= OnDataReceived;
    
            _concreteChannel = concreteChannel;
            _concreteChannel.DataReceived += OnDataReceived;
        }
    
        public void Write(byte[] data)
        {
            _concreteChannel.Write(data);
        }
    
        public byte[] Read()
        {
            return _concreteChannel.Read();
        }
    
        public event EventHandler DataReceived;
    
        private IDataIO _concreteChannel;
    
        private void OnDataReceived(object sender, EventArgs e)
        {
            EventHandler dataReceived = DataReceived;
            if (dataReceived != null)
                dataReceived(this, e);
        }
    }
    

    Finally some code for testing:

    class Test
    {
        public Test()
        {
            _channel = new TcpIO();
    
            _channel.DataReceived += OnDataReceived;
        }
    
        public void SetChannel(IDataIO channel)
        {
            _channel.SetChannel(channel);
    
            // Nothing will change for this "user" of DataIO
            // but now the channel used for transport will be
            // the one defined here
        }
    
        private void OnDataReceived(object sender, EventArgs e)
        {
            // You can use this
            byte[] data = ((IDataIO)sender).Read();
    
            // Or this, the sender is always the concrete
            // implementation that abstracts the strategy in use
            data = _channel.Read();
        }
    
        private DataIO _channel;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface (called Subject ) that has the following method: public void
I have a basic core interface called IComponent. public interface IComponent { void Initialize();
I have a package-private interface called AbstractServer which offers three methods: start() , stop()
I have an interface called IEditor public interface IEditor<T> where T: SpecialObject SpecialObject is
I have the following interface called IAccountService. I also have exactly the same interfaces
I have an interface called Model. It has a method called deserialize() public interface
I have two questions 1) my interface i have interface called IRegister and within
I have an interface that contains a single method called ListAll() using System.Collections.Generic; namespace
I have a scene, called testScene, it works like this: @interface testScene : myScene
I have a base class called Component. I also have 2 interfaces I2DComponent, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.