• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

inheritance

I have 2 classes with much code the same.

How can i use inheritance with some procedures which use the same code in each except for 1 line?

say each has 10 lines with 9 lines the same in each. The different line  is a procedure call with  a different number of parameters.
0
jagguy
Asked:
jagguy
1 Solution
 
daveamourCommented:
What language is this in?
Can you show me your two classes?
0
 
omegaomegaDeveloperCommented:
Hello, jagguy,

There are probably many ways to handle this.  Which is best will probably depend on how you can communicate the value of the parameters required to the classes involved.  The (untested) suggestion shown in the snippet might work if the required parameters are properties of the two classes that you are dealing with.  However, if they are local to the "almost common" procedures, you would need to pass them to "DistinctCall".  Since the parameters are different in the two cases, you might just pass them all and let the inherited routines sort out the ones they need.

Cheers,
Randy

Public MustInherit Class BaseClass
    MustOverride Sub DistinctCall()
    Protected Sub AlmostCommonMethod()
        ' Do some things...
        DistinctCall()
        ' Do the rest of the things...
    End Sub
End Class
 
Public Class InheritedClass1
    Inherits BaseClass
    Public Overrides Sub DistinctCall()
        ' Here you could call the procedure relevant to InheritedClass1.
        ' (The trick here will be to get the values 
        '  of the parameters that you need.)
    End Sub
End Class
 
Public Class InheritedClass2
    Inherits BaseClass
    Public Overrides Sub DistinctCall()
        ' Here you could call the procedure relevant to InheritedClass2.
        ' (The trick here will be to get the values 
        '  of the parameters that you need.)
    End Sub
End Class

Open in new window

0
 
jagguyAuthor Commented:
I am using VB.net and have  a sub procedure I need to inherit but I have 1 line different i the middle of this.

How do i handle inheriting only part of a procedure then add my part then add the rest of the inherited procedure?

0
 
keustermansCommented:
You can do that with a delegate.  I don't know VB that well so Ill give it to you in C# maybe some of the contributors here can translate it into VB for you.

you declare a delegate that follows the signature of the procedure that you execute after the nine lines of code.

You pass the deleagte as a parameter to the function that does the nine lines of code and call the delegate after the nine lines of code have executed.
private delegate void myDelegate(int a,int b,int c,string d);
 
//the function with the 9 lines of code accepts teh delegate as a parameter
 
private void MyNineLinesOfCode( myDelegate dlg)
{
    int a;
    int b;
    int c;
    string d;
    ....
   //My nine lines of code are finished and they assigened valuses 
   //to the variables above
   dlg(a,b,c,d);
}
 
private void someEvent_DoEvent(object sender, EventArgs e)
{
  //function 1 must run after the nine lines of code
  MyNineLinesOfCode(Function1);
 
}
 
private void someOtherEvent_DoEvent(object sender, EventArgs e)
{
  //function 2 must run after the nine lines of code
  MyNineLinesOfCode(Function2);
 
}
 
private void someWickedEvent_DoEvent(object sender, EventArgs e)
{
  //function 3 must run after the nine lines of code
  MyNineLinesOfCode(Function3);
 
}
 
 
diffrent functions come here
 
void Function1(int a, int b, int c, string d)
{
   Do function 1 stuff
}
void Function2(int a, int b, int c, string d)
{
   Do function 2 stuff
}
 
void Function3(int a, int b, int c, string d)
{
   Do function 3 stuff
}

Open in new window

0
 
keustermansCommented:
You need to remember that you need to fprce the same parameter signature for all the functions.

I suggest that you use a parameter signature that will cate for all the possibilities that you are covering with the different paramters that you pass to the functions.

you can then allways fill unwanted parameters with dummy values.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now