delegating - best practice

Hi, I have C# component that has a class as below:
namespace SharedComponent{
class TestResult {
//several members
}
}

In another existing C# application I am referencing this component and I need to instantiate this same class but with an additional identifier as below.

namespace ClientApplication {
class TestResult {
//exact same members as above including methods
//actually the shared component class was created by gleaming
//that from this application!
int PersonID; //additional identifier
            //not suitable to have in the shared component
}
}

In the client application there are several methods that rely on the additional identifier. So it is very tempting for me to emulate a copy constructor and create this object and fill in the additional parameter. This way I can use the existing functions as they are with minimal changes to the class.

Another way could be to add the rest of the details as a reference to the client side implementation.

namespace ClientApplication {
class TestResult {
SharedComponent.TestResult trshared = new SharedComponent.TestResult()
//but this warrants I have my class methods to delegate
//to the sharedcomponent throughout ; example below

internal bool IsFollowUp(ClientApplication.TestResult prevTest)
{
      //a similar method is being used
      //where a function takes the class object as parameter
      trshared.IsFollowUp(prevTest.trshared);
}

int PersonID; //additional identifier

}
}


Which option is better? What is the best practice in this regard?

Environment: VS2008, C#, WinXP/Win7
LVL 1
ol muserTechnology GeneralistAsked:
Who is Participating?
 
Arthur_WoodConnect With a Mentor Commented:
you should create a new class TestResultExtended, which INHERITS from TestResult, and add the new fields/methods to that

namespace ClientApplication {
class TestResultExtended : TestResult  {
internal bool IsFollowUp(ClientApplication.TestResult prevTest)
{
      //a similar method is being used
      //where a function takes the class object as parameter
      trshared.IsFollowUp(prevTest.trshared);
}

int PersonID; //additional identifier
...
}


that way ALL of the fields and methods from Test Result are automatically part of TestResultExtended, without you haiving to do anything.  That is how inheritance works.

AW
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.