Solved

Beginner: combining two nearly identical classes?

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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