Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to call base class methods automatically?

How to call base class methods automatically?

Scheduled Pinned Locked Moved C#
csharpcomtoolshelptutorial
6 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Metal76
    wrote on last edited by
    #1

    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

    S L S J 4 Replies Last reply
    0
    • M Metal76

      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

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      Yes, 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 the protected methods internal or private 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

      1 Reply Last reply
      0
      • M Metal76

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        As 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. regards

        modified on Sunday, June 22, 2008 2:15 PM

        1 Reply Last reply
        0
        • M Metal76

          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

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          Would 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

          1 Reply Last reply
          0
          • M Metal76

            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

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • J Joe Woodbury

              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

              M Offline
              M Offline
              Metal76
              wrote on last edited by
              #6

              I'd like to thank everybody, now I have a whole bunch of new ideas to try out! Best regards, Andrea

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups