Solved

Beginner: combining two nearly identical classes?

Posted on 2011-09-07
4
166 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
  • 2
  • 2
4 Comments
 
LVL 74

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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now