?
Solved

Beginner: combining two nearly identical classes?

Posted on 2011-09-07
4
Medium Priority
?
175 Views
Last Modified: 2012-05-12
How can I combine these two nearly identical classes?
  class MyMsgAAA
  {
    private int msglen = 0;
    private byte[] MyMsg = new byte[500];

    /*constructor*/
    public MyMsgAAA(byte[] ba)
    {
      //Now build the message
      this.add(0x01);
      ...
      add some bytes special to AAA messages
      ...
      //message itself (.add escapes any special bytes)
      for (int i = 0; i < ba.Length; i++)
      {
        this.add(ba[i]);
      }
    }


    /* THE REST HERE IS IDENTICAL */
    public void add(byte b)
    {
      MyMsg[msglen++] = b;
    }
    //Return message
    public byte[] msg
    {
      get
      {
        byte[] target = new byte[this.msglen];
        Array.Copy(MyMsg, target, msglen);
        return target;
      }
    }
  }

Open in new window

  class MyMsgBBB
  {
    private int msglen = 0;
    private byte[] MyMsg = new byte[500];

    /*constructor*/
    public MyMsgBBB(byte[] ba)
    {
      //Now build the message
      this.add(0x01);
      ...
      add some bytes special to BBB messages
      ...
      //message itself (.add escapes any special bytes)
      for (int i = 0; i < ba.Length; i++)
      {
        this.add(ba[i]);
      }
    }


    /* THE REST HERE IS IDENTICAL */
    public void add(byte b)
    {
      MyMsg[msglen++] = b;
    }
    //Return message
    public byte[] msg
    {
      get
      {
        byte[] target = new byte[this.msglen];
        Array.Copy(MyMsg, target, msglen);
        return target;
      }
    }
  }

Open in new window

In general, I want to call
MyMsgXXX(various parameters)

Open in new window

If the parameter types were always different I could just make different constructors for the same class. But what if some messages have the same type of parameter (such as in example above)?
0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36498790
I would say make a common base class which encapsulates the common functionality. You can provide specialized constructors in each child if need be.
0
 

Author Comment

by:deleyd
ID: 36498860
Example?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36498906
Somehow I knew that's what your next question would be  ; )
class MyMsgBase
{
    protected int msglen = 0;
    protected byte[] MyMsg = new byte[500];

    public MyMsgBase()
    {
        //Now build the message
        this.add(0x01);
    }

    /* THE REST HERE IS IDENTICAL */
    public void add(byte b)
    {
        MyMsg[msglen++] = b;
    }

    //Return message
    public byte[] msg
    {
        get
        {
            byte[] target = new byte[this.msglen];
            Array.Copy(MyMsg, target, msglen);
            return target;
        }
    }
}

class MyMsgAAA : MyMsgBase
{
    /*constructor*/
    public MyMsgAAA(byte[] ba) : base()
    {
        //...
        //add some bytes special to AAA messages
        //...

        //message itself (.add escapes any special bytes)
        for (int i = 0; i < ba.Length; i++)
        {
            base.add(ba[i]);
        }
    }
}

class MyMsgBBB : MyMsgBase
{
    /*constructor*/
    public MyMsgBBB(byte[] ba) : base()
    {
        //...
        //add some bytes special to BBB messages
        //...

        //message itself (.add escapes any special bytes)
        for (int i = 0; i < ba.Length; i++)
        {
            this.add(ba[i]);
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:deleyd
ID: 36499213
:)
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question