[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2931
  • Last Modified:

Override the base class constructor in OOP (VB.NET) !

Hi,

I have the following code:

Public Class Employee
    Public Sub New()
        MsgBox("Employee's constructor with no argument")
    End Sub
End Class

Public Class Manager
    Inherits Employee
    Public Sub New(ByVal name As String)
        MsgBox("Manager's constructor - " & name)
    End Sub
End Class

In the form i have the following code :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim manager As New Manager("Kishore")
End Sub

This is equivalent to :

Public Class EmployeeEquivalent
    Public Sub New()
        MsgBox("Employee's constructor with no argument")
    End Sub
End Class

Public Class ManagerEquivalent
    Inherits EmployeeEquivalent
    Public Sub New(ByVal name As String)
        'call must be the first line in our constructor code
        MyBase.New()
        MsgBox("Manager's constructor - " & name)
    End Sub
End Class

In the form i have the following code :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim manager As New Manager("Kishore")
End Sub

BUT I DO NOT WANT TO INVOKE THE BASE CLASS CONSTRUCTOR (Only derived class constructor need to be invoked !). NOW WHAT I HAVE TO DO ? Can you provide a sample code ?

Thanks,
Kishore
0
kishore_peddi
Asked:
kishore_peddi
1 Solution
 
ericwong27Commented:

  'Overridable is not work because constructors are not inherited
   Public Overrides Sub New(ByVal name As String)  < --- Not working
            MsgBox("Manager's constructor - " & name)
    End Sub

 
  Try this ....

  Public Class Employee

    Public Sub New()
        MsgBox("Employee's constructor with no argument")
    End Sub

    Public Sub New(ByVal Flag As Boolean)
        ' Do nothing
    End Sub


End Class

Public Class Manager

    Inherits Employee

    Public Sub New(ByVal name As String)
        MyBase.New(False)
        MsgBox("Manager's constructor - " & name)
    End Sub

End Class
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now