473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inheritance or composition

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
Apr 17 '07 #1
5 1966
"Z" <Z@discussions.microsoft.comwrote in message
news:57**********************************@microsof t.com...
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?
You could use Inheritance and then use the keyword "new" for shadowing
(instead of overriding) the original functions where you want to add or
alter functionality. Your shadow function could then call
base.TheOriginalFunction as needed.
Apr 17 '07 #2
Z,

Why not derive from SerialPort and subscribe to the events yourself?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Z" <Z@discussions.microsoft.comwrote in message
news:57**********************************@microsof t.com...
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

Apr 17 '07 #3
"Z" <Z@discussions.microsoft.comwrote in message
news:57**********************************@microsof t.com...
<snip>
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:
You could do this all easily through composition.
Just create the SerialPort prior to creating your Object.
Pass the SerialPort in as a parameter.
Do all setup operations on the SerialPort directly, but call Send on
your wrapper.

This is a similar relationship to stream/streamwriter.
SerialPort sp = new SerialPort();
// configure the serial port

Have your class take a SerialPort in it's constructor.
MySerialPort wrapper = new MySerialPort(sp);

wrapper.Send(b, 1000);
class MySerialPort
{
private SerialPort serialport;
public MySerialPort (SerialPort serialport)
{
this.serialport = serialport;
}
}
MySerialPort.Send(byte b, int timeout)
{
serialport.Send(b);
StartSomeTimer();
}
MySerialPort.TimerCallback()
{
// tell subscribers the transaction failed
}

Bill
Apr 17 '07 #4
Alberto Poblacion <ea******************************@poblacion.org>
wrote:
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?

You could use Inheritance and then use the keyword "new" for shadowing
(instead of overriding) the original functions where you want to add or
alter functionality. Your shadow function could then call
base.TheOriginalFunction as needed.
That would then fail for any methods which took a SerialPort parameter,
however. The failure would quite possibly be somewhat hard to spot,
too. The "new" modifier for methods should rarely be used other than
for versioning reasons, IMO.

Composition is a neater solution here, I reckon.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 17 '07 #5


"Bill Butler" wrote:
"Z" <Z@discussions.microsoft.comwrote in message
news:57**********************************@microsof t.com...
<snip>
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:

You could do this all easily through composition.
Just create the SerialPort prior to creating your Object.
Pass the SerialPort in as a parameter.
Do all setup operations on the SerialPort directly, but call Send on
your wrapper.

This is a similar relationship to stream/streamwriter.
SerialPort sp = new SerialPort();
// configure the serial port

Have your class take a SerialPort in it's constructor.
MySerialPort wrapper = new MySerialPort(sp);

wrapper.Send(b, 1000);
class MySerialPort
{
private SerialPort serialport;
public MySerialPort (SerialPort serialport)
{
this.serialport = serialport;
}
}
MySerialPort.Send(byte b, int timeout)
{
serialport.Send(b);
StartSomeTimer();
}
MySerialPort.TimerCallback()
{
// tell subscribers the transaction failed
}

Bill
Hi,

Thank you for your replies. I think I will go for composition. I cant use
inheritance
as Nicholas Paldino suggests because I do not want the original event, I
want the event raised upon certain additional conditions.(which I impose).

Thank you again

Apr 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave | last post by:
Hello all, The methodology of policy-based design states that one should inherit from policy classes and, of course, these classes must provide an agreed-upon public interface and semantics. ...
10
by: davidrubin | last post by:
Structural inheritance (inheriting implementation) is equivalent to composition in that a particular method must either call 'Base::foo' or invoke 'base.foo'. Apparantly, The Literature tells us to...
9
by: Code4u | last post by:
My colleagues and I have been discussing techniques for implementing interfaces in C++. We're looking for a mechanism similar to COM's QueryInterface, in which a certain types of objects can be...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
2
by: Wilson | last post by:
Hi, a very simple question. I am trying to understand inheritance using c++ and dont cee how i could use three classes to create an accounting program using inheritance. e.g one class containing...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
6
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
18
by: GD | last post by:
Please remove ability to multiple inheritance in Python 3000. Multiple inheritance is bad for design, rarely used and contains many problems for usual users. Every program can be designed only...
4
by: Pallav singh | last post by:
Hi All i have dout when to use Composition and Inheritance while designing Module in C++ Thanks Pallav
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.