|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Overrides and mybase ??I have seen the following routines in a class and don't understand how they work. For one, why two new() routines? Which is executed? Why not just use one routine? What does the mybase do in this case? Bit confusing! :) Thanks '<remarks/> Public Sub New() MyBase.New() Me.Url = System.Configuration.ConfigurationSettings.AppSettings("ReportServerURL") + "/ReportService.asmx" End Sub '<remarks/> Public Sub New(ByVal ReportServerURL As String) MyBase.New() Me.Url = ReportServerURL + "/ReportService.asmx" End Sub These constructors have 2 different parameters. Someone might want to create
an instance of this class and pass in the URL for the report server, or not. If they don't pass it in, they get the report server from the configuration file. Otherwise, whatever they passed in is used. In both cases, the base class's constructor is called, to make sure that the entire instance gets instantiated properly. The base class constructor may do some work without which the class isn't usable. Show quoteHide quote "TheBee" <The***@discussions.microsoft.com> wrote in message news:AF4C0F2F-7F87-4F45-B62A-F03DA6C705CB@microsoft.com... > Hello, > > I have seen the following routines in a class and don't understand how > they > work. For one, why two new() routines? Which is executed? Why not just > use > one routine? What does the mybase do in this case? Bit confusing! :) > Thanks > > '<remarks/> > Public Sub New() > MyBase.New() > Me.Url = > System.Configuration.ConfigurationSettings.AppSettings("ReportServerURL") > + > "/ReportService.asmx" > End Sub > > '<remarks/> > Public Sub New(ByVal ReportServerURL As String) > MyBase.New() > Me.Url = ReportServerURL + "/ReportService.asmx" > End Sub TheBee wrote:
Show quoteHide quote > Hello, MyBase.New in this case is not strictly necessary, since it is simply> > I have seen the following routines in a class and don't understand how they > work. For one, why two new() routines? Which is executed? Why not just use > one routine? What does the mybase do in this case? Bit confusing! :) Thanks > > '<remarks/> > Public Sub New() > MyBase.New() > Me.Url = > System.Configuration.ConfigurationSettings.AppSettings("ReportServerURL") + > "/ReportService.asmx" > End Sub > > '<remarks/> > Public Sub New(ByVal ReportServerURL As String) > MyBase.New() > Me.Url = ReportServerURL + "/ReportService.asmx" > End Sub calling the base classes default constructor. The only time that MyBase.New is required is if the Base class can't be constucted without parameters - in other words, there is no constructor that doesn't take arguments. You may optionally use it if you want to pass arguments to the base class constuctor... Maybe a small example to make this clear: Option Strict On Option Explicit On Imports System Module Module1 ' Base with only a default constructor Private Class Base Public Sub New(ByVal param As String) Console.WriteLine("Base New - {0}", param) End Sub End Class Private Class Descendant Inherits Base Public Sub New() Console.WriteLine("Descendant New") End Sub Public Sub New(ByVal param As String) Console.WriteLine("Descendant New - {0}", param) End Sub End Class Sub Main() Dim a As New Descendant Dim b As New Descendant("hi") End Sub End Module Now... If you were to add a constructor to the Base class that took parameters... Imports System Module Module1 Private Class Base Public Sub New() Console.WriteLine("Base New") End Sub Public Sub New(ByVal param As String) Console.WriteLine("Base New - {0}", param) End Sub End Class Private Class Descendant Inherits Base Public Sub New() Console.WriteLine("Descendant New") End Sub Public Sub New(ByVal param As String) ' for fun, lets pass on param to the base! MyBase.New(param) Console.WriteLine("Descendant New - {0}", param) End Sub End Class Sub Main() Dim a As New Descendant Dim b As New Descendant("hi") End Sub End Module No, if you remove the Base.New () : Option Strict On Option Explicit On Imports System Module Module1 Private Class Base Public Sub New(ByVal param As String) Console.WriteLine("Base New - {0}", param) End Sub End Class Private Class Descendant Inherits Base Public Sub New() MyBase.New("default") Console.WriteLine("Descendant New") End Sub Public Sub New(ByVal param As String) MyBase.New(param) Console.WriteLine("Descendant New - {0}", param) End Sub End Class Sub Main() Dim a As New Descendant Dim b As New Descendant("hi") End Sub End Module Anyway... Only the last example REQUIRES the call to MyBase.New. Most times, this is simply a convienient way to base arguments to a base class constructor. I think a lot of people still explicitly call MyBase.New() simply because in some of the early VB.NET's (pre 1.0) it was required... -- Tom Shelton [MVP] Tom Shelton wrote:
Show quoteHide quote > TheBee wrote: Crap... I copied in the wrong constructor.. That should simply be:> > Hello, > > > > I have seen the following routines in a class and don't understand how they > > work. For one, why two new() routines? Which is executed? Why not just use > > one routine? What does the mybase do in this case? Bit confusing! :) Thanks > > > > '<remarks/> > > Public Sub New() > > MyBase.New() > > Me.Url = > > System.Configuration.ConfigurationSettings.AppSettings("ReportServerURL") + > > "/ReportService.asmx" > > End Sub > > > > '<remarks/> > > Public Sub New(ByVal ReportServerURL As String) > > MyBase.New() > > Me.Url = ReportServerURL + "/ReportService.asmx" > > End Sub > > MyBase.New in this case is not strictly necessary, since it is simply > calling the base classes default constructor. The only time that > MyBase.New is required is if the Base class can't be constucted without > parameters - in other words, there is no constructor that doesn't take > arguments. You may optionally use it if you want to pass arguments to > the base class constuctor... Maybe a small example to make this clear: > > Option Strict On > Option Explicit On > > Imports System > > Module Module1 > > ' Base with only a default constructor > Private Class Base > Public Sub New(ByVal param As String) > Console.WriteLine("Base New - {0}", param) > End Sub > End Class > ' Base with only a default constructor Public Sub New() Console.WriteLine("Base New") End Sub Sorry for the confusion! -- Tom Shelton [MVP] Thank you very much for the time you spent replying. It is greatly
appreciated. So the person who wrote the code really didn't need to do both. They could have just used the first one? So you can duplicate a name of a routine many times without issue? Does compiler than automatically choose which to use based on parameters passed? What if both routines have same parameters with types? What happens then? Thanks again Show quoteHide quote "Tom Shelton" <t**@mtogden.com> wrote in message news:1150497072.093756.179810@f6g2000cwb.googlegroups.com... > > Tom Shelton wrote: >> TheBee wrote: >> > Hello, >> > >> > I have seen the following routines in a class and don't understand how >> > they >> > work. For one, why two new() routines? Which is executed? Why not >> > just use >> > one routine? What does the mybase do in this case? Bit confusing! :) >> > Thanks >> > >> > '<remarks/> >> > Public Sub New() >> > MyBase.New() >> > Me.Url = >> > System.Configuration.ConfigurationSettings.AppSettings("ReportServerURL") >> > + >> > "/ReportService.asmx" >> > End Sub >> > >> > '<remarks/> >> > Public Sub New(ByVal ReportServerURL As String) >> > MyBase.New() >> > Me.Url = ReportServerURL + "/ReportService.asmx" >> > End Sub >> >> MyBase.New in this case is not strictly necessary, since it is simply >> calling the base classes default constructor. The only time that >> MyBase.New is required is if the Base class can't be constucted without >> parameters - in other words, there is no constructor that doesn't take >> arguments. You may optionally use it if you want to pass arguments to >> the base class constuctor... Maybe a small example to make this clear: >> >> Option Strict On >> Option Explicit On >> >> Imports System >> >> Module Module1 >> >> ' Base with only a default constructor >> Private Class Base >> Public Sub New(ByVal param As String) >> Console.WriteLine("Base New - {0}", param) >> End Sub >> End Class >> > > Crap... I copied in the wrong constructor.. That should simply be: > > ' Base with only a default constructor > Public Sub New() > Console.WriteLine("Base New") > End Sub > > Sorry for the confusion! > > -- > Tom Shelton [MVP] > The Bee,
| So you can duplicate a name of a routine many Yes this is known as overloading.| times without issue? | Does compiler than automatically choose which to use Yes the compiler looks at the number of parameters & their types to decide | based on parameters passed? which overload to call. | What if both routines have same parameters with The signatures of the methods (routines) need to be unique on number & type | types? of parameters. Overloading on return value or parameter name will cause a compile error. Advanced information: ..NET 2.0 (VS 2005) allows you to overload generic classes on the number of type parameters, however you cannot overload a generic simply on the constraint. For example: System.Nullable - has shared methods pertaining to all nullable types. System.Nullable(Of T) - creates a specific nullable type, such as Nullable(Of Integer) & Nullable(Of Double). The reason for the two Nullable types is to simplify calling of the methods on System.Nullable... -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "The Bee" <The***@discussions.microsoft.com> wrote in message System.Configuration.ConfigurationSettings.AppSettings("ReportServerURL")news:OzOmV7ZkGHA.4212@TK2MSFTNGP03.phx.gbl... | Thank you very much for the time you spent replying. It is greatly | appreciated. | | So the person who wrote the code really didn't need to do both. They could | have just used the first one? So you can duplicate a name of a routine many | times without issue? Does compiler than automatically choose which to use | based on parameters passed? What if both routines have same parameters with | types? What happens then? | | Thanks again | | "Tom Shelton" <t**@mtogden.com> wrote in message | news:1150497072.093756.179810@f6g2000cwb.googlegroups.com... | > | > Tom Shelton wrote: | >> TheBee wrote: | >> > Hello, | >> > | >> > I have seen the following routines in a class and don't understand how | >> > they | >> > work. For one, why two new() routines? Which is executed? Why not | >> > just use | >> > one routine? What does the mybase do in this case? Bit confusing! :) | >> > Thanks | >> > | >> > '<remarks/> | >> > Public Sub New() | >> > MyBase.New() | >> > Me.Url = | >> > Show quoteHide quote | >> > + | >> > "/ReportService.asmx" | >> > End Sub | >> > | >> > '<remarks/> | >> > Public Sub New(ByVal ReportServerURL As String) | >> > MyBase.New() | >> > Me.Url = ReportServerURL + "/ReportService.asmx" | >> > End Sub | >> | >> MyBase.New in this case is not strictly necessary, since it is simply | >> calling the base classes default constructor. The only time that | >> MyBase.New is required is if the Base class can't be constucted without | >> parameters - in other words, there is no constructor that doesn't take | >> arguments. You may optionally use it if you want to pass arguments to | >> the base class constuctor... Maybe a small example to make this clear: | >> | >> Option Strict On | >> Option Explicit On | >> | >> Imports System | >> | >> Module Module1 | >> | >> ' Base with only a default constructor | >> Private Class Base | >> Public Sub New(ByVal param As String) | >> Console.WriteLine("Base New - {0}", param) | >> End Sub | >> End Class | >> | > | > Crap... I copied in the wrong constructor.. That should simply be: | > | > ' Base with only a default constructor | > Public Sub New() | > Console.WriteLine("Base New") | > End Sub | > | > Sorry for the confusion! | > | > -- | > Tom Shelton [MVP] | > | |
Memory Leaks
Assigning Nullables to each other RichTextFormat Guru needed Flash SWF in VS2005 .net? Date time format Can we seperate the Bussiness Logic and User Interface? Printing over network... JavaScript focus() function not working in "start without debugging" How to "Archive" a text file with multiple handles open vb.net form that behaves like Access? |
|||||||||||||||||||||||