jagguy
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.
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.
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
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
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?
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.
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
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you show me your two classes?