Re: Overrides and mybase ??




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]

.



Relevant Pages

  • Re: Overrides and mybase ??
    ... I have seen the following routines in a class and don't understand how ... Public Sub New ... calling the base classes default constructor. ... MyBase.New is required is if the Base class can't be constucted without ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Overrides and mybase ??
    ... an instance of this class and pass in the URL for the report server, ... In both cases, the base class's constructor is called, to make sure that the ... I have seen the following routines in a class and don't understand how ... Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Create Pages on the fly using IHttpHandler. Need advice
    ... public sub Child() ... MasterPageFile = "Parent.master" ... Pages, controls and masterpages are all class deep down...and if something fires BEFORE PreInit, the constructor must be it:) ... aspx files are handled by a built-in handler, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Problem with assigning new variables to duplicate form
    ... 'Identical' means they have the same controls and they access the same ... > Public Sub AddToList ... >> In common bas module I have routines, ...
    (microsoft.public.vb.general.discussion)
  • Re: Sub class constructor
    ... If you want to use parent constructors when instanciating your child class, ... public sub New ... constructors not parent class constructor; ...
    (microsoft.public.dotnet.languages.vb)