Solved

Beginner: combining two nearly identical classes?

Posted on 2011-09-07
4
172 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 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 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