Link to home
Start Free TrialLog in
Avatar of jagguy
jagguyFlag for Australia

asked on

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.
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

What language is this in?
Can you show me your two classes?
Avatar of omegaomega
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

Avatar of jagguy

ASKER

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?

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

ASKER CERTIFIED SOLUTION
Avatar of Marcus Keustermans
Marcus Keustermans
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial