Overloaded New in MustInherit Class ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe HM

    Overloaded New in MustInherit Class ...

    Hello -

    I have a question regarding overloading the New() in a MustInherit
    Class. The following show the code in question ...

    MustInherit Class cAbstract
    Public MustOverride Sub print()

    Protected mName As String

    Public Sub New() ' Why do I have to override this?
    mName = "N/A"
    End Sub

    Public Sub New(ByVal aName As String)
    mName = aName
    End Sub
    End Class

    Class cChildOfAbstrac t
    Inherits cAbstract

    Public Overrides Sub print()
    Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
    mName & "'")
    End Sub
    End Class

    ....
    Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") ' Why is
    this not working?
    ....

    First of all, I have to override the New(). Is this because I
    overloaded it with the New(ByVal aName As String)?

    Then I'm trying to call the overloaded constructor when I instantiate
    the cChildOfAbstrac t but I cannot do that because apparently I have
    too many arguments. This works if I override the New(ByVal aName As
    String) in the child class but why do I have to do that? I thought I
    would inherit that from the base class.

    Thanks!
    Joe
  • James Hahn

    #2
    Re: Overloaded New in MustInherit Class ...

    What do you mean by 'have to override the New()' - what is the message you
    are getting? Constructors aren't ordinary methods and are inherited
    differently.

    If the base class has no constructors or has a a paramaterless constructor
    then you don't need a constructor. But if the base class does not have a
    paramaterless constructor then your derived class must have a constructor
    and the very first line of the constructor must be a call to the base class
    constructor. Perhaps it was this requirement (not met in your sample code)
    that made it appear that you were required to have a New(). It all depends
    on what the base class's constructors are.

    "Joe HM" <unixverse@yaho o.comwrote in message
    news:e0acbe71-34a0-4d80-bd2c-b7af41208783@r6 6g2000hsg.googl egroups.com...
    Hello -
    >
    I have a question regarding overloading the New() in a MustInherit
    Class. The following show the code in question ...
    >
    MustInherit Class cAbstract
    Public MustOverride Sub print()
    >
    Protected mName As String
    >
    Public Sub New() ' Why do I have to override this?
    mName = "N/A"
    End Sub
    >
    Public Sub New(ByVal aName As String)
    mName = aName
    End Sub
    End Class
    >
    Class cChildOfAbstrac t
    Inherits cAbstract
    >
    Public Overrides Sub print()
    Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
    mName & "'")
    End Sub
    End Class
    >
    ...
    Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") ' Why is
    this not working?
    ...
    >
    First of all, I have to override the New(). Is this because I
    overloaded it with the New(ByVal aName As String)?
    >
    Then I'm trying to call the overloaded constructor when I instantiate
    the cChildOfAbstrac t but I cannot do that because apparently I have
    too many arguments. This works if I override the New(ByVal aName As
    String) in the child class but why do I have to do that? I thought I
    would inherit that from the base class.
    >
    Thanks!
    Joe

    Comment

    • Family Tree Mike

      #3
      Re: Overloaded New in MustInherit Class ...

      To the best of my knowledge, constructors are not inheritted in VB.Net. Add
      this constructor to your class cChildOfAbstrac t to accomplish what I think
      you want:

      Public Sub New(ByVal s As String)
      MyBase.New(s)
      End Sub

      I don't think you need the default constructor in your base class, at least
      in what I was running.

      "Joe HM" <unixverse@yaho o.comwrote in message
      news:e0acbe71-34a0-4d80-bd2c-b7af41208783@r6 6g2000hsg.googl egroups.com...
      Hello -
      >
      I have a question regarding overloading the New() in a MustInherit
      Class. The following show the code in question ...
      >
      MustInherit Class cAbstract
      Public MustOverride Sub print()
      >
      Protected mName As String
      >
      Public Sub New() ' Why do I have to override this?
      mName = "N/A"
      End Sub
      >
      Public Sub New(ByVal aName As String)
      mName = aName
      End Sub
      End Class
      >
      Class cChildOfAbstrac t
      Inherits cAbstract
      >
      Public Overrides Sub print()
      Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
      mName & "'")
      End Sub
      End Class
      >
      ...
      Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") ' Why is
      this not working?
      ...
      >
      First of all, I have to override the New(). Is this because I
      overloaded it with the New(ByVal aName As String)?
      >
      Then I'm trying to call the overloaded constructor when I instantiate
      the cChildOfAbstrac t but I cannot do that because apparently I have
      too many arguments. This works if I override the New(ByVal aName As
      String) in the child class but why do I have to do that? I thought I
      would inherit that from the base class.
      >
      Thanks!
      Joe

      Comment

      • Chris Dunaway

        #4
        Re: Overloaded New in MustInherit Class ...

        On Oct 27, 9:30 am, Joe HM <unixve...@yaho o.comwrote:
        Hello -
        >
        I have a question regarding overloading the New() in a MustInherit
        Class. The following show the code in question ...
        >
        MustInherit Class cAbstract
        Public MustOverride Sub print()
        >
        Protected mName As String
        >
        Public Sub New() ' Why do I have to override this?
        mName = "N/A"
        End Sub
        >
        Public Sub New(ByVal aName As String)
        mName = aName
        End Sub
        End Class
        >
        Class cChildOfAbstrac t
        Inherits cAbstract
        >
        Public Overrides Sub print()
        Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
        mName & "'")
        End Sub
        End Class
        >
        ...
        Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name") ' Why is
        this not working?
        ...
        >
        First of all, I have to override the New(). Is this because I
        overloaded it with the New(ByVal aName As String)?
        >
        Then I'm trying to call the overloaded constructor when I instantiate
        the cChildOfAbstrac t but I cannot do that because apparently I have
        too many arguments. This works if I override the New(ByVal aName As
        String) in the child class but why do I have to do that? I thought I
        would inherit that from the base class.
        >
        Thanks!
        Joe
        Constructors are not inherited. You will have to provide the child
        constructors that you need.

        Chris

        Comment

        • Joe HM

          #5
          Re: Overloaded New in MustInherit Class ...

          Hello -

          The following will cause a "Class 'cChild' must declare a 'Sub New'
          because its base class 'cParent' does not have an accessible 'Sub New'
          that can be called with no arguments."

          MustInherit Class cParent
          Public Sub New(ByVal aArgument As String)
          MyBase.New()
          End Sub
          End Class

          Class cChild
          Inherits cParent
          End Class

          I thought that by default every class has a parameterless constructor
          but I guess I was mistaken. Looks like I have to spell it out.

          Thanks,
          Joe



          On Oct 27, 6:05 pm, "James Hahn" <jh...@yahoo.co mwrote:
          What do you mean by 'have to override the New()' - what is the message you
          are getting?  Constructors aren't ordinary methods and are inherited
          differently.
          >
          If the base class has no constructors or has a a paramaterless constructor
          then you don't need a constructor. But if the base class does not have a
          paramaterless constructor then your derived class must have a constructor
          and the very first line of the constructor must be a call to the base class
          constructor. Perhaps it was this requirement (not met in your sample code)
          that made it appear that you were required to have a New(). It all depends
          on what the base class's constructors are.
          >
          "Joe HM" <unixve...@yaho o.comwrote in message
          >
          news:e0acbe71-34a0-4d80-bd2c-b7af41208783@r6 6g2000hsg.googl egroups.com...
          >
          >
          >
          Hello -
          >
          I have a question regarding overloading the New() in a MustInherit
          Class.  The following show the code in question ...
          >
          MustInherit Class cAbstract
             Public MustOverride Sub print()
          >
             Protected mName As String
          >
             Public Sub New()  ' Why do I have to override this?
                 mName = "N/A"
             End Sub
          >
             Public Sub New(ByVal aName As String)
                 mName = aName
             End Sub
          End Class
          >
          Class cChildOfAbstrac t
             Inherits cAbstract
          >
             Public Overrides Sub print()
                 Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
          mName & "'")
             End Sub
          End Class
          >
          ...
          Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name")   ' Why is
          this not working?
          ...
          >
          First of all, I have to override the New().  Is this because I
          overloaded it with the New(ByVal aName As String)?
          >
          Then I'm trying to call the overloaded constructor when I instantiate
          the cChildOfAbstrac t but I cannot do that because apparently I have
          too many arguments.  This works if I override the New(ByVal aName As
          String) in the child class but why do I have to do that?  I thought I
          would inherit that from the base class.
          >
          Thanks!
          Joe- Hide quoted text -
          >
          - Show quoted text -

          Comment

          • Joe HM

            #6
            Re: Overloaded New in MustInherit Class ...

            Hello -

            I guess I just realized that I don't need an interface to the
            overloaded New() with the parameter in the base class. The following
            works ...

            MustInherit Class cParent
            ....
            End Class

            Class cChild
            Inherits cParent

            Public Sub New(ByVal aArgument As String)
            MyBase.New()
            ...
            End Sub
            End Class

            ....

            Dim lDummy As cParent
            lDummy = New cChild("")

            Thanks,
            Joe



            On Oct 27, 6:59 pm, "Family Tree Mike"
            <FamilyTreeM... @ThisOldHouse.c omwrote:
            To the best of my knowledge, constructors are not inheritted in VB.Net.  Add
            this constructor to your class cChildOfAbstrac t to accomplish what I think
            you want:
            >
             Public Sub New(ByVal s As String)
              MyBase.New(s)
             End Sub
            >
            I don't think you need the default constructor in your base class, at least
            in what I was running.
            >
            "Joe HM" <unixve...@yaho o.comwrote in message
            >
            news:e0acbe71-34a0-4d80-bd2c-b7af41208783@r6 6g2000hsg.googl egroups.com...
            >
            >
            >
            Hello -
            >
            I have a question regarding overloading the New() in a MustInherit
            Class.  The following show the code in question ...
            >
            MustInherit Class cAbstract
               Public MustOverride Sub print()
            >
               Protected mName As String
            >
               Public Sub New()  ' Why do I have to override this?
                   mName = "N/A"
               End Sub
            >
               Public Sub New(ByVal aName As String)
                   mName = aName
               End Sub
            End Class
            >
            Class cChildOfAbstrac t
               Inherits cAbstract
            >
               Public Overrides Sub print()
                   Console.WriteLi ne("cChildOfAbs tract.print() mName = '" &
            mName & "'")
               End Sub
            End Class
            >
            ...
            Dim lInstanceOfChil d As New cChildOfAbstrac t("My Name")   ' Why is
            this not working?
            ...
            >
            First of all, I have to override the New().  Is this because I
            overloaded it with the New(ByVal aName As String)?
            >
            Then I'm trying to call the overloaded constructor when I instantiate
            the cChildOfAbstrac t but I cannot do that because apparently I have
            too many arguments.  This works if I override the New(ByVal aName As
            String) in the child class but why do I have to do that?  I thought I
            would inherit that from the base class.
            >
            Thanks!
            Joe- Hide quoted text -
            >
            - Show quoted text -

            Comment

            • Tom Shelton

              #7
              Re: Overloaded New in MustInherit Class ...

              On 2008-10-28, Joe HM <unixverse@yaho o.comwrote:
              Hello -
              >
              The following will cause a "Class 'cChild' must declare a 'Sub New'
              because its base class 'cParent' does not have an accessible 'Sub New'
              that can be called with no arguments."
              >
              MustInherit Class cParent
              Public Sub New(ByVal aArgument As String)
              MyBase.New()
              End Sub
              End Class
              >
              Class cChild
              Inherits cParent
              End Class
              >
              I thought that by default every class has a parameterless constructor
              but I guess I was mistaken. Looks like I have to spell it out.
              >
              Thanks,
              Joe
              >
              The compiler will only generate a default constructor if you have not
              explicitly added one to the class. As soon as you add the constructor:

              Public Sub New (ByVal aArgument As String)

              The compiler assumes you have explicitly stated your desires, and no longer
              generates a default constructor. This is how it works in pretty much all
              statically compiled languages... And it makes sense, because a lot of times a
              default no argument constructor doesn't make sense for a class.


              --
              Tom Shelton

              Comment

              Working...