public abstract class CommTalker
{
public virtual int GetDeviceID()
{
// do comm
}
}
public class Device1 : CommTalker
{
public virtual int GetSomething()
{
// do comm
}
public virtual void SetSomething(int value)
{
// do comm
}
}
public class Device2 : CommTalker
{
public virtual int GetInfo()
{
// do comm
}
public virtual void SetInfo(int value)
{
// do comm
}
}
I need to make a simulator for each device that overrides each command so that no comm port communication is done. The problem I have is that each simulator must extend the Device class, but this means I have to do overrides for each function in the base CommTalker class for each simulator.
public class Device1Sim : Device1
{
public override int GetDeviceID()
{
return 234534;
}
int something = 5;
public override int GetSomething()
{
return something;
}
public override void SetSomething(int value)
{
something = value;
}
}
public class Device2Sim : Device2
{
public override int GetDeviceID()
{
return 234534;
}
int info = 6;
public override int GetInfo()
{
return info;
}
public override void SetInfo(int value)
{
info = value;
}
}
In this code, CommTalker only has 1 function but in the real code it has about 30, so what I'd like to do would be something like this (to avoid duplicate code):
public class CommTalkerSim : CommTalker
{
public override int GetDeviceID()
{
return 234534;
}
}
public class Device1Sim : Device1, CommTalkerSim
{
int something = 5;
public override int GetSomething()
{
return something;
}
public override void SetSomething(int value)
{
something = value;
}
}
public class Device2Sim : Device2, CommTalkerSim
{
int info = 6;
public override int GetInfo()
{
return info;
}
public override void SetInfo(int value)
{
info = value;
}
}
I know C# doesn't support multiple inheritance and interfaces can't have code. Can anybody think of another way to do this that C# will allow?

New Topic/Question
Reply




MultiQuote






|