Hi,
I want to create a class that sends/receives data from the serial port.
There is such a class in .NET v 2.0 but what I need is a serial port with
some extra features. For example I want my class to raise a certain event not
only when data has been received but also upon some additional condition. If
the class I want to extend was an Windows.Forms class I could write this:
class MyButton:Button
{
protected override void OnClick(...)
{
if ( IsMonday ()) // my button can be clicked only on
Mondays
{
base.OnClick(...);
// raise the event by calling subscribers
}
}
}
The problem is that the SerialPort class has no overrides, only events. This
means that I have to use it like this:
class MySerialPort
{
private SerialPort sp;
..
}
But this means that I have to add functions like this:
MySerialPort.Open()
{
sp.Open();
}
which is a lot of code.
So I decided to set sp public:
class MySerialPort
{
public SerialPort sp;
..
}
Now I can write:
MySerialPort msp=new MySerialPort;
msp.sp.Opne();
The problem here is that I want to have a function Send() in my port that
not only sends a byte but also specifies for example a timeout, ie the caller
of this function expects an answer within certain time. This means that I
have to intercept the calls to SerialPort.Send() and which means I have to go
back to have SerialPort as private:
MySerialPort.Send(byte b, int timeout)
{
SerialPort.Send(b);
StartSomeTimer();
}
MySerialPort.TimerCallback()
{
// tell subscribers the transaction failed
}
Is this the only way to extend the functionality of a class like SerialPort,
which has no overridables, by composition (and lot of code)? Can inheritanc
ebe used somehow?
Thank you