Re: Overrides and mybase ??




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

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]

.



Relevant Pages

  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: hyperlinks in a paragraph
    ... Public Sub Discover(ByVal MStr As String) ... Public Sub ProcPara ... Dim HL As Hyperlink ...
    (microsoft.public.word.vba.general)
  • Re: Concurrency violation
    ... Public Sub SendAllSMS() ... Dim ds As New Data.DataSet ... Dim NewDat As String ... Public Property ODBC_ConnectionStringAs String ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Using XSLT for sending emails
    ... dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean ... BusinessLogic.EmailTemplate.SendEmailMessage(String emailTo, String ... Public Sub New ... Dim pr As DataAccess.DataAccessBase.ProcedureResults ...
    (microsoft.public.dotnet.general)

Loading