Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Constructor in MustInherit Class

Constructor in MustInherit Class

Scheduled Pinned Locked Moved .NET (Core and Framework)
visual-studiohelpquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Soumya92
    wrote on last edited by
    #1

    I have a MustInherit class A, something like

    Public MustInherit Class A

    Protected A as Integer
    
    Public Sub New(ByVal X As Integer)
        A=X
    End Sub
    
    Public MustOverride Sub B()
    

    End Class

    and I want the declared constructor to be used for any derived classes. However, in derived classes, like

    Public Class D
    Inherits A

    Public Sub B()
        A+=2
    End Sub
    

    End Class

    VS gives me an error, saying that there is no constructor in A that can be called without any arguments. Is there any way I can inherit the constructor?

    ~ Soumya92

    L W I 3 Replies Last reply
    0
    • S Soumya92

      I have a MustInherit class A, something like

      Public MustInherit Class A

      Protected A as Integer
      
      Public Sub New(ByVal X As Integer)
          A=X
      End Sub
      
      Public MustOverride Sub B()
      

      End Class

      and I want the declared constructor to be used for any derived classes. However, in derived classes, like

      Public Class D
      Inherits A

      Public Sub B()
          A+=2
      End Sub
      

      End Class

      VS gives me an error, saying that there is no constructor in A that can be called without any arguments. Is there any way I can inherit the constructor?

      ~ Soumya92

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Soumya92 wrote:

      VS gives me an error, saying that there is no constructor in A that can be called without any arguments.

      Let's add that constructor then, and override the B sub;

      Public Class D
      Inherits A

      Public Sub New(ByVal X As Integer)
          MyBase.New(X)
      End Sub
      
      Public Overloads Overrides Sub B()
          A = A + 2
      End Sub
      

      End Class

      I are Troll :suss:

      1 Reply Last reply
      0
      • S Soumya92

        I have a MustInherit class A, something like

        Public MustInherit Class A

        Protected A as Integer
        
        Public Sub New(ByVal X As Integer)
            A=X
        End Sub
        
        Public MustOverride Sub B()
        

        End Class

        and I want the declared constructor to be used for any derived classes. However, in derived classes, like

        Public Class D
        Inherits A

        Public Sub B()
            A+=2
        End Sub
        

        End Class

        VS gives me an error, saying that there is no constructor in A that can be called without any arguments. Is there any way I can inherit the constructor?

        ~ Soumya92

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        If class D inherits From Class A and Class A has no paramterless constructor, then you have to have a constructor with at least the same signature as the original constructor and call MyBase.New(X). Your class D can have more parameters but one has to be an integer, which must be passed to the original constructor. If you think about it, it makes sense as you need to create an instance of both classes to create an instance of Class D.

        S 1 Reply Last reply
        0
        • W Wayne Gaylard

          If class D inherits From Class A and Class A has no paramterless constructor, then you have to have a constructor with at least the same signature as the original constructor and call MyBase.New(X). Your class D can have more parameters but one has to be an integer, which must be passed to the original constructor. If you think about it, it makes sense as you need to create an instance of both classes to create an instance of Class D.

          S Offline
          S Offline
          Soumya92
          wrote on last edited by
          #4

          The whole idea was to _not_ declare the constructor again. I agree that it makes sense to construct the base class object first, but I want the base class constructor to be used if I do not specify one in the derived class.

          ~ Soumya92

          L 1 Reply Last reply
          0
          • S Soumya92

            The whole idea was to _not_ declare the constructor again. I agree that it makes sense to construct the base class object first, but I want the base class constructor to be used if I do not specify one in the derived class.

            ~ Soumya92

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Soumya92 wrote:

            The whole idea was to _not_ declare the constructor again.

            Sorry[^] :sigh:

            I are Troll :suss:

            1 Reply Last reply
            0
            • S Soumya92

              I have a MustInherit class A, something like

              Public MustInherit Class A

              Protected A as Integer
              
              Public Sub New(ByVal X As Integer)
                  A=X
              End Sub
              
              Public MustOverride Sub B()
              

              End Class

              and I want the declared constructor to be used for any derived classes. However, in derived classes, like

              Public Class D
              Inherits A

              Public Sub B()
                  A+=2
              End Sub
              

              End Class

              VS gives me an error, saying that there is no constructor in A that can be called without any arguments. Is there any way I can inherit the constructor?

              ~ Soumya92

              I Offline
              I Offline
              Ian Shlasko
              wrote on last edited by
              #6

              In other words, you want to be able to create a new "D" using the constructor defined in "A"? Short answer... You can't. Constructors don't inherit in .NET... You have to explicitly call them in a constructor in "D".

              Public Sub New(ByVal X as Integer)
              MyBase.New(X)
              End Sub

              If you have a lot of these subclasses, then you might want to move the initialization code out of the constructor and into an Initialize(X) sub that you could call after instantiating it. Easy enough to set a boolean flag so it can only be called once.

              Proud to have finally moved to the A-Ark. Which one are you in?
              Author of the Guardians Saga (Sci-Fi/Fantasy novels)

              S 1 Reply Last reply
              0
              • I Ian Shlasko

                In other words, you want to be able to create a new "D" using the constructor defined in "A"? Short answer... You can't. Constructors don't inherit in .NET... You have to explicitly call them in a constructor in "D".

                Public Sub New(ByVal X as Integer)
                MyBase.New(X)
                End Sub

                If you have a lot of these subclasses, then you might want to move the initialization code out of the constructor and into an Initialize(X) sub that you could call after instantiating it. Easy enough to set a boolean flag so it can only be called once.

                Proud to have finally moved to the A-Ark. Which one are you in?
                Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                S Offline
                S Offline
                Soumya92
                wrote on last edited by
                #7

                OK. Thanks a lot for your quick replies. We'll call the question resolved, then?

                ~ Soumya92

                I 1 Reply Last reply
                0
                • S Soumya92

                  OK. Thanks a lot for your quick replies. We'll call the question resolved, then?

                  ~ Soumya92

                  I Offline
                  I Offline
                  Ian Shlasko
                  wrote on last edited by
                  #8

                  Yep, sorry it's not the answer you wanted.

                  Proud to have finally moved to the A-Ark. Which one are you in?
                  Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups