Superconstructors?

What's the general syntax for using inheritance in VB Say I've got ClassSuper and ClassSub, ClassSub inherits ClassSuper. How do you access the super constructor How do you access values in the super class

Answer this question

Superconstructors?

  • shido

    Public Class SuperClass

    Public Sub New()

    'Does Something

    End Sub

    End Class

    Public Class SubClass

    Inherits SuperClass

    Public Sub New()

    'Do More

    End Sub

    End Class

     

    What is your end result objective   A contructor is not overridable and if you inherit from the super class you subclass when instatiated will "dosomething" from the base class and will "do More" from its own contructor....You may wish to create overridable methods so the sub class can access the parent class methods...



  • suzy_mathew

    Hi,

    You want to use MyBase.New() to call the SuperClass's constructor. It has to be the first statement in the constructor.

    Public Class SubClass
    Inherits SuperClass

    Public d As INteger
    Public e As INteger

    Public Sub New(a As Integer, b As Integer, c As Integer, _
    d As Integer, e As Integer)
    MyBase.New(a,b,c)
    Me.d = d
    Me.e = e
    End Sub
    End Class

    Public Class SuperClass
    Public a As Integer
    Public b As INteger
    Public c As Integer

    Public Sub New(a As INteger, b As Integer, c AS Integer)
    Me.a = a
    Me.b = b
    Me.c = c
    End Sub
    End Class


  • Michael Schroeder

    Thank ya, exactly what I needed.

    And just to make sure, MyBase always goes up one level, right So if I had SuperClass, SubClass and SubSubClass, to access SuperClass's constructor you'd use MyBase.MyBase.New()


  • Bear23

    I think Dick Donny's close... here, let me type what I need to do in Java syntax and hopefully that'll express what I need.

    Public Class SuperClass{

    int a, b, c;

    public SuperClass(int a,int b,int c){

    this.a=a;

    this.b=b;

    this.c=c;

    }

    }

    Public Class SubClass extends SuperClass{

    int d, e;

    public SubClass(int a,int b,int c,int d,int e){

    super(a,b,c);

    this.d=d;

    this.e=e;

    }

    }

    Does that make sense


  • Brandon Waskiewicz

    joynerCN wrote:
    What's the general syntax for using inheritance in VB Say I've got ClassSuper and ClassSub, ClassSub inherits ClassSuper. How do you access the super constructor How do you access values in the super class

    I have assumed that your use of 'super' to refer to a class relates to the ClassSuper type.

    Class ClassSuper : Sub New()

    Class ClassSub : Inherits ClassSuper : Sub New() ... MyBase.New

    In the inheriting class, use of the MyBase keyword exposes methods that the inheriting class has scope to access in the base class (ClassSub). Access to the base class ctor can only be obtained from the ctor of the inheriting class.

    Is that what you were looking for, or did I completely miss the point :)

    Richard


  • potatosnketchup

    joyner,

    You can only access one level up with MyBase.New(). If you want to call up many levels, use Protected Sub New's for the same effect. Protected means they can only be called by children. Here is an example:

    Public Class SubSubClass
    Inherits SubClass

    Public Sub New()
    MyBase.New("Special Functionality")
    End Sub
    End Class

    Public Class SubClass
    Inherits SuperClass

    Public d As Integer
    Public e As Integer

    Protected Sub New(ByVal arg As String)
    MyBase.New(arg) ' Pass args straight up to parent
    End Sub

    Public Sub New(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer, _
    ByVal d As Integer, ByVal e As Integer)
    MyBase.New(a, b, c)
    Me.d = d
    Me.e = e
    End Sub
    End Class

    Public Class SuperClass
    Public a As Integer
    Public b As Integer
    Public c As Integer

    Protected Sub New(ByVal arg As String)
    ' put special functionality for your grandchildren here
    End Sub

    Public Sub New(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
    Me.a = a
    Me.b = b
    Me.c = c
    End Sub
    End Class

    Hope that helps.
    Matt


  • Superconstructors?