|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
overides/loads usage#1 i am looking through some code and have a question. The class I am looking at is a derived class that has one constructor (new). it is coded as: public sub new () .. .. .. end sub i see that the base class has a new procedure with no arguments. The arguments in both base and derived classes are the same. Is the new procedure in the derived class shadowing or overriding the new procedure in base class? I understand that it would be easy to tell if the coder specified keyword of overrides or shadows, but they didn't. #2 In another derived class I see two new procedures. One with no parameters like one in base class and the other with two parameters. I understand that the constructor is overloaded, but is the new with no parameters shadowing or overriding the new in base class. How can one know if shadowing is being used or if overriding is being used when no keyword is specified in procedure declaration? Thanks in advance. > i see that the base class has a new procedure with no arguments. The The answer is neither. Constructors are "special", in that, they neither > arguments in both base and derived classes are the same. Is the new > procedure in the derived class shadowing or overriding the new procedure > in base class? I understand that it would be easy to tell if the coder > specified keyword of overrides or shadows, but they didn't. shadow or override other constructors from their base classes. Take this example: Public Class A Sub New() End Sub End Class Public Class B Inherits A Sub New() End Sub End Class If you were to make an instance of "B", the constructor from class "A" will fire and then the constructor from class "B". > Nether again. The constructor in the derived class that takes the > #2 In another derived class I see two new procedures. One with no > parameters like one in base class and the other with two parameters. I > understand that the constructor is overloaded, but is the new with no > parameters shadowing or overriding the new in base class. parameters is overloading the constructor in the derived class AND the constructor in the base class. > How can one know if shadowing is being used or if overriding is being used Simple. To use Shadowing or Overloading in VB.NET, the Shadows and > when no keyword is specified in procedure declaration? Overrideable and Overrides keywords MUST be specified. If you don't see those words, they are not being used. Show quoteHide quote > > Thanks in advance. > Thank you very much for responding.
#1 ok. I understand now. Is the new constructor the only procedure that executes the new() in base before the derived? #2 But why not always use overloading or shadowing rather than overriding? It seems that you can accomplish what you need with overloading and shadowing especially since to override (as you said) requires the overridable keyword in base class. When do I use overloading verses shadowing? Thanks again. Show quoteHide quote "Scott M." <s-mar@nospam.nospam> wrote in message news:uuxZK6QqGHA.4408@TK2MSFTNGP04.phx.gbl... >> i see that the base class has a new procedure with no arguments. The >> arguments in both base and derived classes are the same. Is the new >> procedure in the derived class shadowing or overriding the new procedure >> in base class? I understand that it would be easy to tell if the coder >> specified keyword of overrides or shadows, but they didn't. > > The answer is neither. Constructors are "special", in that, they neither > shadow or override other constructors from their base classes. Take this > example: > > Public Class A > Sub New() > > End Sub > End Class > > Public Class B > Inherits A > Sub New() > > End Sub > End Class > > If you were to make an instance of "B", the constructor from class "A" > will fire and then the constructor from class "B". > >> >> #2 In another derived class I see two new procedures. One with no >> parameters like one in base class and the other with two parameters. I >> understand that the constructor is overloaded, but is the new with no >> parameters shadowing or overriding the new in base class. > > Nether again. The constructor in the derived class that takes the > parameters is overloading the constructor in the derived class AND the > constructor in the base class. > >> How can one know if shadowing is being used or if overriding is being >> used when no keyword is specified in procedure declaration? > > Simple. To use Shadowing or Overloading in VB.NET, the Shadows and > Overrideable and Overrides keywords MUST be specified. If you don't see > those words, they are not being used. > >> >> Thanks in advance. >> > > > #1 ok. I understand now. Is the new constructor the only procedure that Automatically, yes. You could make a base class's method run prior to the > executes the new() in base before the derived? derived class's method by inserting a MyBase.someMethod call in the derived class's code. Think of it this way, when you call any class's constructor, it is as if the first line of code in that constructor is: MyBase.New. > #2 But why not always use overloading or shadowing rather than overriding? Overloading and Shadowing are 2 very different things. Overloading is for > It seems that you can accomplish what you need with overloading and > shadowing especially since to override (as you said) requires the > overridable keyword in base class. When do I use overloading verses > shadowing? when you want to have the same method name as an existing method, but want it to be accesbale with different arguments than the original ALONG with the original. The user can call either overloaded version they wish. The overloaded version would still provide the same essential functionality, but with different parameters. A common use is for default values vs. user defined values. Take for example: Public Sub New x = 10 End Sub Public Sub New(Amount As Integer) x = Amount End Sub Both of these will cause x's value to change but one does it with a user supplied value vs. a default value. This is overloading. Overriding would mean that one TAKES THE PLACE of the other and only one could be called by the user. Overloading allows EITHER to be called. It's Overriding and Shadowing that are similar, but with one BIG difference. Marking a class member as Overridable means that in derived classes, the member can be re-writted (with the Overrides keyword) to take the place of the Overridable member in the base class. The key here is that the derived class can only re-write the member if the original member was declared "Overridable". You need permission to use Overriding. Sometimes you need to override something that was not originally marked as Overridable and the original source code is not available. Then you re-write the member in the derived class with the keyword "Shadows" to force your derived member to override the base class member. This, however, must only be done when necessary and with great care when it is done because you could break some basic functionality by replacing a member with a completely new implementation. Shadowing is overriding, just without the prior permission to do so. -Scott Show quoteHide quote > > Thanks again. > > "Scott M." <s-mar@nospam.nospam> wrote in message > news:uuxZK6QqGHA.4408@TK2MSFTNGP04.phx.gbl... >>> i see that the base class has a new procedure with no arguments. The >>> arguments in both base and derived classes are the same. Is the new >>> procedure in the derived class shadowing or overriding the new procedure >>> in base class? I understand that it would be easy to tell if the coder >>> specified keyword of overrides or shadows, but they didn't. >> >> The answer is neither. Constructors are "special", in that, they neither >> shadow or override other constructors from their base classes. Take this >> example: >> >> Public Class A >> Sub New() >> >> End Sub >> End Class >> >> Public Class B >> Inherits A >> Sub New() >> >> End Sub >> End Class >> >> If you were to make an instance of "B", the constructor from class "A" >> will fire and then the constructor from class "B". >> >>> >>> #2 In another derived class I see two new procedures. One with no >>> parameters like one in base class and the other with two parameters. I >>> understand that the constructor is overloaded, but is the new with no >>> parameters shadowing or overriding the new in base class. >> >> Nether again. The constructor in the derived class that takes the >> parameters is overloading the constructor in the derived class AND the >> constructor in the base class. >> >>> How can one know if shadowing is being used or if overriding is being >>> used when no keyword is specified in procedure declaration? >> >> Simple. To use Shadowing or Overloading in VB.NET, the Shadows and >> Overrideable and Overrides keywords MUST be specified. If you don't see >> those words, they are not being used. >> >>> >>> Thanks in advance. >>> >> >> > > Thank you very much Scott for your excellent explanations. It has helped
greatly. Have a good day. Show quoteHide quote "Scott M." <s-mar@nospam.nospam> wrote in message news:eXmOi6UqGHA.2464@TK2MSFTNGP03.phx.gbl... >> #1 ok. I understand now. Is the new constructor the only procedure that >> executes the new() in base before the derived? > > Automatically, yes. You could make a base class's method run prior to the > derived class's method by inserting a MyBase.someMethod call in the > derived class's code. > > Think of it this way, when you call any class's constructor, it is as if > the first line of code in that constructor is: MyBase.New. > >> #2 But why not always use overloading or shadowing rather than >> overriding? It seems that you can accomplish what you need with >> overloading and shadowing especially since to override (as you said) >> requires the overridable keyword in base class. When do I use >> overloading verses shadowing? > > Overloading and Shadowing are 2 very different things. Overloading is for > when you want to have the same method name as an existing method, but want > it to be accesbale with different arguments than the original ALONG with > the original. The user can call either overloaded version they wish. The > overloaded version would still provide the same essential functionality, > but with different parameters. A common use is for default values vs. > user defined values. Take for example: > > Public Sub New > x = 10 > End Sub > > Public Sub New(Amount As Integer) > x = Amount > End Sub > > Both of these will cause x's value to change but one does it with a user > supplied value vs. a default value. This is overloading. Overriding > would mean that one TAKES THE PLACE of the other and only one could be > called by the user. Overloading allows EITHER to be called. > > It's Overriding and Shadowing that are similar, but with one BIG > difference. Marking a class member as Overridable means that in derived > classes, the member can be re-writted (with the Overrides keyword) to take > the place of the Overridable member in the base class. The key here is > that the derived class can only re-write the member if the original member > was declared "Overridable". You need permission to use Overriding. > > Sometimes you need to override something that was not originally marked as > Overridable and the original source code is not available. Then you > re-write the member in the derived class with the keyword "Shadows" to > force your derived member to override the base class member. This, > however, must only be done when necessary and with great care when it is > done because you could break some basic functionality by replacing a > member with a completely new implementation. Shadowing is overriding, > just without the prior permission to do so. > > -Scott > > >> >> Thanks again. >> >> "Scott M." <s-mar@nospam.nospam> wrote in message >> news:uuxZK6QqGHA.4408@TK2MSFTNGP04.phx.gbl... >>>> i see that the base class has a new procedure with no arguments. The >>>> arguments in both base and derived classes are the same. Is the new >>>> procedure in the derived class shadowing or overriding the new >>>> procedure in base class? I understand that it would be easy to tell if >>>> the coder specified keyword of overrides or shadows, but they didn't. >>> >>> The answer is neither. Constructors are "special", in that, they >>> neither shadow or override other constructors from their base classes. >>> Take this example: >>> >>> Public Class A >>> Sub New() >>> >>> End Sub >>> End Class >>> >>> Public Class B >>> Inherits A >>> Sub New() >>> >>> End Sub >>> End Class >>> >>> If you were to make an instance of "B", the constructor from class "A" >>> will fire and then the constructor from class "B". >>> >>>> >>>> #2 In another derived class I see two new procedures. One with no >>>> parameters like one in base class and the other with two parameters. I >>>> understand that the constructor is overloaded, but is the new with no >>>> parameters shadowing or overriding the new in base class. >>> >>> Nether again. The constructor in the derived class that takes the >>> parameters is overloading the constructor in the derived class AND the >>> constructor in the base class. >>> >>>> How can one know if shadowing is being used or if overriding is being >>>> used when no keyword is specified in procedure declaration? >>> >>> Simple. To use Shadowing or Overloading in VB.NET, the Shadows and >>> Overrideable and Overrides keywords MUST be specified. If you don't see >>> those words, they are not being used. >>> >>>> >>>> Thanks in advance. >>>> >>> >>> >> >> > > Happy to help. Good luck.
Show quoteHide quote "RdS" <rds@nospam.nospam> wrote in message news:ulKz5CdqGHA.2068@TK2MSFTNGP03.phx.gbl... > Thank you very much Scott for your excellent explanations. It has helped > greatly. Have a good day. > > "Scott M." <s-mar@nospam.nospam> wrote in message > news:eXmOi6UqGHA.2464@TK2MSFTNGP03.phx.gbl... >>> #1 ok. I understand now. Is the new constructor the only procedure >>> that executes the new() in base before the derived? >> >> Automatically, yes. You could make a base class's method run prior to >> the derived class's method by inserting a MyBase.someMethod call in the >> derived class's code. >> >> Think of it this way, when you call any class's constructor, it is as if >> the first line of code in that constructor is: MyBase.New. >> >>> #2 But why not always use overloading or shadowing rather than >>> overriding? It seems that you can accomplish what you need with >>> overloading and shadowing especially since to override (as you said) >>> requires the overridable keyword in base class. When do I use >>> overloading verses shadowing? >> >> Overloading and Shadowing are 2 very different things. Overloading is >> for when you want to have the same method name as an existing method, but >> want it to be accesbale with different arguments than the original ALONG >> with the original. The user can call either overloaded version they >> wish. The overloaded version would still provide the same essential >> functionality, but with different parameters. A common use is for >> default values vs. user defined values. Take for example: >> >> Public Sub New >> x = 10 >> End Sub >> >> Public Sub New(Amount As Integer) >> x = Amount >> End Sub >> >> Both of these will cause x's value to change but one does it with a user >> supplied value vs. a default value. This is overloading. Overriding >> would mean that one TAKES THE PLACE of the other and only one could be >> called by the user. Overloading allows EITHER to be called. >> >> It's Overriding and Shadowing that are similar, but with one BIG >> difference. Marking a class member as Overridable means that in derived >> classes, the member can be re-writted (with the Overrides keyword) to >> take the place of the Overridable member in the base class. The key here >> is that the derived class can only re-write the member if the original >> member was declared "Overridable". You need permission to use >> Overriding. >> >> Sometimes you need to override something that was not originally marked >> as Overridable and the original source code is not available. Then you >> re-write the member in the derived class with the keyword "Shadows" to >> force your derived member to override the base class member. This, >> however, must only be done when necessary and with great care when it is >> done because you could break some basic functionality by replacing a >> member with a completely new implementation. Shadowing is overriding, >> just without the prior permission to do so. >> >> -Scott >> >> >>> >>> Thanks again. >>> >>> "Scott M." <s-mar@nospam.nospam> wrote in message >>> news:uuxZK6QqGHA.4408@TK2MSFTNGP04.phx.gbl... >>>>> i see that the base class has a new procedure with no arguments. The >>>>> arguments in both base and derived classes are the same. Is the new >>>>> procedure in the derived class shadowing or overriding the new >>>>> procedure in base class? I understand that it would be easy to tell >>>>> if the coder specified keyword of overrides or shadows, but they >>>>> didn't. >>>> >>>> The answer is neither. Constructors are "special", in that, they >>>> neither shadow or override other constructors from their base classes. >>>> Take this example: >>>> >>>> Public Class A >>>> Sub New() >>>> >>>> End Sub >>>> End Class >>>> >>>> Public Class B >>>> Inherits A >>>> Sub New() >>>> >>>> End Sub >>>> End Class >>>> >>>> If you were to make an instance of "B", the constructor from class "A" >>>> will fire and then the constructor from class "B". >>>> >>>>> >>>>> #2 In another derived class I see two new procedures. One with no >>>>> parameters like one in base class and the other with two parameters. >>>>> I understand that the constructor is overloaded, but is the new with >>>>> no parameters shadowing or overriding the new in base class. >>>> >>>> Nether again. The constructor in the derived class that takes the >>>> parameters is overloading the constructor in the derived class AND the >>>> constructor in the base class. >>>> >>>>> How can one know if shadowing is being used or if overriding is being >>>>> used when no keyword is specified in procedure declaration? >>>> >>>> Simple. To use Shadowing or Overloading in VB.NET, the Shadows and >>>> Overrideable and Overrides keywords MUST be specified. If you don't see >>>> those words, they are not being used. >>>> >>>>> >>>>> Thanks in advance. >>>>> >>>> >>>> >>> >>> >> >> > > Scott M. wrote:
<snip> > Sometimes you need to override something that was not originally marked as <snip>> Overridable and the original source code is not available. Then you > re-write the member in the derived class with the keyword "Shadows" to force > your derived member to override the base class member. This, however, must > only be done when necessary and with great care when it is done because you > could break some basic functionality by replacing a member with a completely > new implementation. Shadowing is overriding, just without the prior > permission to do so. Notice that in terms of the execution path there is a tremendous difference bvetween Shadows and Overrides. Methods from the *base class* will call an overriden method in a derived class, but not shadowed ones: Class Base Overridable Sub Step1 Debug.Print("Base.Step1") End Sub Overridable Sub Step2 Debug.Print("Base.Step2") End Sub Sub DoIt Debug.Print("Base.DoIt") Step1 Step2 End Sub End Class Class Derived Inherits Base Overrides Sub Step1 Debug.Print("Derived.Step1") End Sub Shadows Sub Step2 Debug.Print("Derived.Step2") End Sub Shadows Sub DoIt Debug.Print("Derived.DoIt") Step1 Step2 End Sub End Class '... Dim A As Base = New Derived A.DoIt The code fragment above will print: Base.DoIt Derived.Step1 Base.Step2 As you can see, even though the class is an instance of Derived, the original Base.DoIt method is being called, because we declared A as a Base. Because Derived shadows the original method, it would be necessary to declare A as Derived to access Derived.DoIt. Then the Base.DoIt method is able to call Derived.Step1. This is the magic of overriden (aka 'virtual') methods: they allow for a base class' method to be *altered* by a derived class. Finally, we see that even though Base.Step2 is virtual (overridable), the original method of Base is called, because Derived didn't *override* it, just shadowed it. In other words, overridable methods (with their respective overriding in derived classes) allow for the modification of the original behavior of the base class. It's as if you left from point A expecting to get at point B and magically arrived at point C because of a shortcut that was put in the road without your knowledge. Shadowing, on the other side, just reuses a name from a base class, effectivelly hiding that original name at the new scope. It's as if, to get to point C, you had to go to a point named A, only that nobody seems to remember that there was once a point A that lead to point B instead, as if the original point A had never existed... Regards, Branco.
AS400 - Ado.Net from Vb.Net Slow Query Times
Can I make my Form visible during a debug session? Database Connection Problem. Please Help Problems using Me.Scale in Framework 2.0 enforce event calling on inherited classes Is this a bug: 2.2 - 0.4 = 1.8 but 1.2 - 0.4 = 0.8000001 ? object reference not set to an instance of an object error Multiple executescalar() Commenting Out Controls In HTML View Convert Array of Objects to Generics List? |
|||||||||||||||||||||||