delegates for selecting between two, or more, classes

  • Thread starter Anders Eriksson
  • Start date
A

Anders Eriksson

I need to be able to switch between two different types of IO.
HardIO which communicates with actual hardware on the computer
and SoftIO which uses Tcp/Ip to communicate with an another
device. The selection of which type of IO is done on upstart and
will not change during the run of the program.

I have these classes(see below) and using
delegate int getPort(int port);
delegate int setPort(int port, int mask);

getPort = HardIO.GetPort;
setPort = HardIO.SetPort;
or
getPort = SoftIO.GetPort;
setPort = SoftIO.SetPort;


I can connect the delegates to the chosen class methods.

My question: Is there someway of connecting a delegate to the class?
In this sample there are only two methods but if I have a class with
20 methods I need 20 delegates...
There surely must be a better way?

// Anders

interface IBitIO
{
int GetPort(int port);
bool SetPort(int port, int mask);
}

public class HardIO : IBitIO
{
public static int GetPort(int port)
{
// implementation
}
public static bool SetPort(int port, int mask)
{
// implementation
}
}

public class SoftIO : IBitIO
{
public static int GetPort(int port)
{
// implementation
}
public static bool SetPort(int port, int mask)
{
// implementation
}
}
 
M

Marcel Müller

I need to be able to switch between two different types of IO.
HardIO which communicates with actual hardware on the computer
and SoftIO which uses Tcp/Ip to communicate with an another
device. The selection of which type of IO is done on upstart and
will not change during the run of the program.

The solution of your problem is a common interface of your classes and
optional a factory method.

I have these classes(see below) and using
delegate int getPort(int port);
delegate int setPort(int port, int mask);
[...]

My question: Is there someway of connecting a delegate to the class?

Yes, but you are trying to reinvent the wheel. You implement virtual
methods or interfaces (basically the same) by hand. Don't do that unless
you need to.

There surely must be a better way?
interface IBitIO
{
int GetPort(int port);
bool SetPort(int port, int mask);
}

You already have it! :)
public class HardIO : IBitIO
{
public static int GetPort(int port)
^^^^^^
This won't work. Interface implementations must not be static.


All you need is:

IBitIO portHandler;

portHandler = new HardIO(); // or new whoever implements IBitIO


To call the method do

portHandler.GetPort(port);


Marcel
 
A

Anders Eriksson

You already have it! :)

^^^^^^
This won't work. Interface implementations must not be static.
I didn't test this code, just assumed that it would work.
TIL; don't assume, test!
All you need is:

IBitIO portHandler;

portHandler = new HardIO(); // or new whoever implements IBitIO


To call the method do

portHandler.GetPort(port);


Marcel

Yes, of cause!
got a bit blinded with the delegate thing...

Thank you very much!

// Anders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top