How to call base class methods automatically?
-
Hi, as you might know from an older post (here) I'm working on a communication protocol driver in C#. Protocol messages can be of two types (commands and responses) so I chose the following class structure:
public abstract class Message
{
// Properties & methods representing fields common to all messages
protected byte[] CompileCommonPacketPart()
{
// Compiles a part of the packet from the common fields
}
}public abstract class Command : Message
{
// Additional fields and methods available for all commands
protected byte[] CompileCommandPacketPart()
{
// Compiles a part of the packet from command-specific data
}
}public abstract class Response : Message
{
// Additional fields and methods available for all responses
protected byte[] CompileResponsePacketPart()
{
// Compiles a part of the packet from response-specific data
}
}public class MyCommand : Command
{
// Concrete command class, adds data
// specific to this particular command
public byte[] CompilePacket()
{
// Compiles the generic parts of the command
CompileCommonPacketPart();
CompileCommandPacketPart();// Adds parts specific to this command ... }
}
As you see, at the moment
CompilePacket()
explicitly includes calls to the base methods in order to compile the generic parts of the packet, and every time I define a new command I must manually insert these calls. This could be error-prone (I have to deal with a lot of commands...) and I feel that there should be a way to factor out this common behaviour... Is there any way to perform these calls automatically (not explicitly)? Thanks in advance for your support! Andrea -
Hi, as you might know from an older post (here) I'm working on a communication protocol driver in C#. Protocol messages can be of two types (commands and responses) so I chose the following class structure:
public abstract class Message
{
// Properties & methods representing fields common to all messages
protected byte[] CompileCommonPacketPart()
{
// Compiles a part of the packet from the common fields
}
}public abstract class Command : Message
{
// Additional fields and methods available for all commands
protected byte[] CompileCommandPacketPart()
{
// Compiles a part of the packet from command-specific data
}
}public abstract class Response : Message
{
// Additional fields and methods available for all responses
protected byte[] CompileResponsePacketPart()
{
// Compiles a part of the packet from response-specific data
}
}public class MyCommand : Command
{
// Concrete command class, adds data
// specific to this particular command
public byte[] CompilePacket()
{
// Compiles the generic parts of the command
CompileCommonPacketPart();
CompileCommandPacketPart();// Adds parts specific to this command ... }
}
As you see, at the moment
CompilePacket()
explicitly includes calls to the base methods in order to compile the generic parts of the packet, and every time I define a new command I must manually insert these calls. This could be error-prone (I have to deal with a lot of commands...) and I feel that there should be a way to factor out this common behaviour... Is there any way to perform these calls automatically (not explicitly)? Thanks in advance for your support! AndreaYes, it can be error-prone, but there isn't a way to automatically call the base class methods. You could implement a virtual method on
Command
that handles this for you and then in the derived classes override that method instead. It would make your classes look like this:public abstract class Command : Message
{
protected byte[] CompileCommandPacketPart()
{
}public virtual byte[] CompilePacket()
{
// Compiles the generic parts of the command
CompileCommonPacketPart();
CompileCommandPacketPart();
}
}public class MyCommand : Command
{
public override byte[] CompilePacket()
{
base.CompilePacket();
// Adds parts specific to this command
}
}This simplifies things a bit in that you only have to call
base.CompilePacket()
. Thinking about it a bit more, you might be able to do this:public abstract class Command : Message
{
protected byte[] CompileCommandPacketPartInternal()
{
}protected abstract byte[] CompileCommandPacketPart()
{
}protected byte[] CompilePacket()
{
// Compiles the generic parts of the command
CompileCommonPacketPart();
CompileCommandPacketPartInternal();
CompileCommandPacketPart();
}
}public class MyCommand : Command
{
public override byte[] CompileCommandPacketPart()
{
// Adds parts specific to this command
}This will work as long as the order of calls in
Command.CompilePacket()
will always be the same. In either case, you might want to make some of theprotected
methodsinternal
orprivate
instead.Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
Hi, as you might know from an older post (here) I'm working on a communication protocol driver in C#. Protocol messages can be of two types (commands and responses) so I chose the following class structure:
public abstract class Message
{
// Properties & methods representing fields common to all messages
protected byte[] CompileCommonPacketPart()
{
// Compiles a part of the packet from the common fields
}
}public abstract class Command : Message
{
// Additional fields and methods available for all commands
protected byte[] CompileCommandPacketPart()
{
// Compiles a part of the packet from command-specific data
}
}public abstract class Response : Message
{
// Additional fields and methods available for all responses
protected byte[] CompileResponsePacketPart()
{
// Compiles a part of the packet from response-specific data
}
}public class MyCommand : Command
{
// Concrete command class, adds data
// specific to this particular command
public byte[] CompilePacket()
{
// Compiles the generic parts of the command
CompileCommonPacketPart();
CompileCommandPacketPart();// Adds parts specific to this command ... }
}
As you see, at the moment
CompilePacket()
explicitly includes calls to the base methods in order to compile the generic parts of the packet, and every time I define a new command I must manually insert these calls. This could be error-prone (I have to deal with a lot of commands...) and I feel that there should be a way to factor out this common behaviour... Is there any way to perform these calls automatically (not explicitly)? Thanks in advance for your support! AndreaAs Scott already said, there's no way to automatically call a base class method. One solution would be to use some kind of hierarchy, in case the methods are always called in the same order:
public abstract class Message { // Properties & methods representing fields common to all messages protected **virtual** byte\[\] CompilePacket() { // Compiles a part of the packet from the common fields } } public abstract class Command : Message { // Additional fields and methods available for all commands protected **override** byte\[\] CompilePacket() { **base.CompilePacket();** // Compiles a part of the packet from command-specific data } } public abstract class Response : Message { // Additional fields and methods available for all responses protected **override** byte\[\] CompilePacket() { **base.CompilePacket();** // Compiles a part of the packet from response-specific data } } public class MyCommand : Command { // Concrete command class, adds data // specific to this particular command public byte\[\] CompileCustomPacket() { **base.CompilePacket();** // Adds parts specific to this command } }
So you will only have to call
base.CompilePacket()
, everything else will be handled by the inheritance hierarchy. regardsmodified on Sunday, June 22, 2008 2:15 PM
-
Hi, as you might know from an older post (here) I'm working on a communication protocol driver in C#. Protocol messages can be of two types (commands and responses) so I chose the following class structure:
public abstract class Message
{
// Properties & methods representing fields common to all messages
protected byte[] CompileCommonPacketPart()
{
// Compiles a part of the packet from the common fields
}
}public abstract class Command : Message
{
// Additional fields and methods available for all commands
protected byte[] CompileCommandPacketPart()
{
// Compiles a part of the packet from command-specific data
}
}public abstract class Response : Message
{
// Additional fields and methods available for all responses
protected byte[] CompileResponsePacketPart()
{
// Compiles a part of the packet from response-specific data
}
}public class MyCommand : Command
{
// Concrete command class, adds data
// specific to this particular command
public byte[] CompilePacket()
{
// Compiles the generic parts of the command
CompileCommonPacketPart();
CompileCommandPacketPart();// Adds parts specific to this command ... }
}
As you see, at the moment
CompilePacket()
explicitly includes calls to the base methods in order to compile the generic parts of the packet, and every time I define a new command I must manually insert these calls. This could be error-prone (I have to deal with a lot of commands...) and I feel that there should be a way to factor out this common behaviour... Is there any way to perform these calls automatically (not explicitly)? Thanks in advance for your support! AndreaWould the "Template Method" design pattern work?
public abstract class Message
{
public byte[] CompilePacket()
{
CompileCommonPacketPart();
CompileCommandPacketPart();
CompilePacketInternal();
}protected abstract byte\[\] CompilePacketInternal();
}
public class MyCommand : Command
{
protected override byte[] CompilePacketInternal() {}
}Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
Hi, as you might know from an older post (here) I'm working on a communication protocol driver in C#. Protocol messages can be of two types (commands and responses) so I chose the following class structure:
public abstract class Message
{
// Properties & methods representing fields common to all messages
protected byte[] CompileCommonPacketPart()
{
// Compiles a part of the packet from the common fields
}
}public abstract class Command : Message
{
// Additional fields and methods available for all commands
protected byte[] CompileCommandPacketPart()
{
// Compiles a part of the packet from command-specific data
}
}public abstract class Response : Message
{
// Additional fields and methods available for all responses
protected byte[] CompileResponsePacketPart()
{
// Compiles a part of the packet from response-specific data
}
}public class MyCommand : Command
{
// Concrete command class, adds data
// specific to this particular command
public byte[] CompilePacket()
{
// Compiles the generic parts of the command
CompileCommonPacketPart();
CompileCommandPacketPart();// Adds parts specific to this command ... }
}
As you see, at the moment
CompilePacket()
explicitly includes calls to the base methods in order to compile the generic parts of the packet, and every time I define a new command I must manually insert these calls. This could be error-prone (I have to deal with a lot of commands...) and I feel that there should be a way to factor out this common behaviour... Is there any way to perform these calls automatically (not explicitly)? Thanks in advance for your support! AndreaPublicly expose the base method only. Have the base method call a protected [pure] virtual method. The derived classes now implement the latter method, not the first.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
Publicly expose the base method only. Have the base method call a protected [pure] virtual method. The derived classes now implement the latter method, not the first.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke