|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
A class structure questionHello,
I have a problem trying to figure out the following design issue. I have a base class and a number of classes derived from that base. A method in the base class covers 90% of the functionality required for all classes, however I need to have that 10% functionality that is left in the derived classes. What I would like to do is have the method in the base class be able to call the method in the derived classes, it should do this according to the actual class that was instantiated. For example: The base class is "bClass", and there are two derived classes "Class1" and "Class2". The method "Test" in bClass needs to call "OtherMethod" in either Class1 or Class2. If I instantiate an object of type Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can this be done? Thanks, Sid. How about using the "MyClass" keyword?
Show quoteHide quote "Sid Price" <s**@nowhere.com> wrote in message news:uYqjytmvGHA.4512@TK2MSFTNGP05.phx.gbl... > Hello, > > I have a problem trying to figure out the following design issue. I have a > base class and a number of classes derived from that base. A method in the > base class covers 90% of the functionality required for all classes, > however I need to have that 10% functionality that is left in the derived > classes. What I would like to do is have the method in the base class be > able to call the method in the derived classes, it should do this > according to the actual class that was instantiated. > > For example: The base class is "bClass", and there are two derived classes > "Class1" and "Class2". The method "Test" in bClass needs to call > "OtherMethod" in either Class1 or Class2. If I instantiate an object of > type Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can > this be done? > > Thanks, > Sid. > > Sid Price wrote:
Show quoteHide quote > Hello, It almost sounds like you need to create an abstract class (MustInherit> > I have a problem trying to figure out the following design issue. I have a > base class and a number of classes derived from that base. A method in the > base class covers 90% of the functionality required for all classes, however > I need to have that 10% functionality that is left in the derived classes. > What I would like to do is have the method in the base class be able to call > the method in the derived classes, it should do this according to the actual > class that was instantiated. > > For example: The base class is "bClass", and there are two derived classes > "Class1" and "Class2". The method "Test" in bClass needs to call > "OtherMethod" in either Class1 or Class2. If I instantiate an object of type > Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can this > be done? > > Thanks, > Sid. in VB.NET). It would look something like: Option Explicit On Option Strict On Imports System Module Module1 Sub Main() Dim bc1 As TheBaseClass = New ChildClass1 Dim bc2 As TheBaseClass = New ChildClass2 bc1.TheSubThatCallsTheMethod() bc2.TheSubThatCallsTheMethod() End Sub Private MustInherit Class TheBaseClass Protected MustOverride Sub TheAbstractMethod() Public Sub TheSubThatCallsTheMethod() ' do stuff Me.TheAbstractMethod() 'do more stuff End Sub End Class Private Class ChildClass1 Inherits TheBaseClass Protected Overrides Sub TheAbstractMethod() Console.WriteLine("Inside ChildClass1") End Sub End Class Private Class ChildClass2 Inherits TheBaseClass Protected Overrides Sub TheAbstractMethod() Console.WriteLine("Inside ChildClass2") End Sub End Class End Module HTH -- Tom Shelton [MVP] "Tom Shelton" <t**@mtogden.com> wrote in message That looks like it does exactley what I need, thank you Tom.news:1155445098.677094.160760@74g2000cwt.googlegroups.com... > It almost sounds like you need to create an abstract class (MustInherit > in VB.NET). > -- > Tom Shelton [MVP] > Sid. Price,
Reading your message I had the same idea of Tom about the mustInherit. However at the end I had the idea that this would not be always needed, you are only asking about the possibilitie to override (as Tom shows nicely) or to shadow members. However without that mustInherit is Tom's sample as well very good. See for Shadows this, you use it the same as overriding but the base class is than not any more used at all for that member (be aware by this at what level you use it, how you use interfaces and how you cast). http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeyShadows.asp I hope this helps, Cor Show quoteHide quote "Sid Price" <s**@nowhere.com> schreef in bericht news:uYqjytmvGHA.4512@TK2MSFTNGP05.phx.gbl... > Hello, > > I have a problem trying to figure out the following design issue. I have a > base class and a number of classes derived from that base. A method in the > base class covers 90% of the functionality required for all classes, > however I need to have that 10% functionality that is left in the derived > classes. What I would like to do is have the method in the base class be > able to call the method in the derived classes, it should do this > according to the actual class that was instantiated. > > For example: The base class is "bClass", and there are two derived classes > "Class1" and "Class2". The method "Test" in bClass needs to call > "OtherMethod" in either Class1 or Class2. If I instantiate an object of > type Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can > this be done? > > Thanks, > Sid. > > "Sid Price" <s**@nowhere.com> ha scritto nel messaggio Define OtherMethod as abstract in bClass (that should be named ClassBase and news:uYqjytmvGHA.4512@TK2MSFTNGP05.phx.gbl... > For example: The base class is "bClass", and there are two derived classes > "Class1" and "Class2". The method "Test" in bClass needs to call > "OtherMethod" in either Class1 or Class2. If I instantiate an object of > type Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can > this be done? not bClass). In Class1 and Class2 if you need special behavior do an override of OtherMethod. Sid Price wrote:
> I have a problem trying to figure out the following design issue. I have a Two ways:> base class and a number of classes derived from that base. A method in the > base class covers 90% of the functionality required for all classes, however > I need to have that 10% functionality that is left in the derived classes. > What I would like to do is have the method in the base class be able to call > the method in the derived classes, it should do this according to the actual > class that was instantiated. > > For example: The base class is "bClass", and there are two derived classes > "Class1" and "Class2". The method "Test" in bClass needs to call > "OtherMethod" in either Class1 or Class2. If I instantiate an object of type > Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can this > be done? (1) If you really want two methods ("Test" and "OtherMethod"): Class bClass Public Sub Test() ' 90% of test Code Me.OtherMethod() End Sub Public Overridable Sub OtherMethod() ' Do Nothing End Sub End Class Class Class1 Public Overrides Sub OtherMethod() ' Do the other 10% End Sub End Class (2) Or, if you're not too bothered about having two methods, you can use what I call "extending" (overriding, but then re-using the base class' implementation), as in Class bClass Public Overridable Sub Test() ' 90% of the job End Sub End Class Class Class1 Public Overrides Sub Test() MyBase.Test() ' 90% ' Now, do the other 10% End Sub End Class HTH, Phill W. > Two ways: Having a method that does nothing is a bad design idea. It's like putting a > (1) If you really want two methods ("Test" and "OtherMethod"): > > Class bClass > Public Sub Test() > ' 90% of test Code > Me.OtherMethod() > End Sub > Public Overridable Sub OtherMethod() > ' Do Nothing > End Sub > End Class button on a remote control that does nothing. > (2) Or, if you're not too bothered about having two methods, you can use Extending is what everyone else calls it as well.> what I call "extending" (overriding, but then re-using the base class' > implementation), as in Scott M. wrote:
Show quoteHide quote >> Two ways: In itself, it does nothing, but it provides a "hook" for a derived class >> (1) If you really want two methods ("Test" and "OtherMethod"): >> >> Class bClass >> Public Sub Test() >> ' 90% of test Code >> Me.OtherMethod() >> End Sub >> Public Overridable Sub OtherMethod() >> ' Do Nothing >> End Sub >> End Class > > Having a method that does nothing is a bad design idea. It's like putting a > button on a remote control that does nothing. to do something else .. er .. instead. >> (2) Or, if you're not too bothered about having two methods, you can use That's a relief. There's enough terminology around already without me >> what I call "extending" (overriding, but then re-using the base class' >> implementation), as in > > Extending is what everyone else calls it as well. inventing any more of it ;-) Regards, Phill W. >> Having a method that does nothing is a bad design idea. It's like This is still a bad design interface. Since we don't know *if* a derived >> putting a button on a remote control that does nothing. > > In itself, it does nothing, but it provides a "hook" for a derived class > to do something else .. er .. instead. class will even want to do *something* with this method, we are adding a method for the sake of adding a method. |
|||||||||||||||||||||||