[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Beginner: combining two nearly identical classes?

Posted on 2011-09-07
4
Medium Priority
?
177 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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