Christiaan
Christiaan

Reputation: 669

I cant inherit form my parent class

I'm having a problem that I cant figure out, I'm trying to code a payroll system with vb.net 2013, I must use a console application.

I'm using classes to calculate the salary depending on the amount of weeks that the employee's worked, but the problem is that I cannot inherit from my parent class, I keep on getting this error and i don't know how to fix it.

Error 2 Class 'Employee_Payment_System.Employee' has no accessible 'Sub New' and cannot be inherited.

Whenever i try to add a 'sub new' I get this

Error 3 'Public Sub New()' has multiple definitions with identical signatures.  

Here is my code so far

Public Class Employee

#Region "Private Declarations"

    Private EMP_FullName As String
    Private EMP_LastName As String
    Private EMP_Salary As Double
    Private EMP_Number As Integer
    Private EMP_Address As String

#End Region
'The values are obtained from the public properties  coded below 
    Private Sub New()
        EMP_FullName = ""
        EMP_LastName = ""
        EMP_Salary = 0.0
        EMP_Address = ""
    End Sub

    Private Sub New(ByVal FullName As String, ByVal LastName As String, ByVal Address As String, ByVal Salary As Double, ByVal Number As Integer)

        EMP_FullName = FullName
        EMP_LastName = LastName
        EMP_Address = Address
        EMP_Number = Number
        EMP_Salary = Salary

    End Sub
' the properties gets their values from the console
    Public Property FullName() As String
        Get
            Return EMP_FullName
        End Get
        Set(value As String)
            EMP_FullName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return EMP_LastName
        End Get
        Set(value As String)
            EMP_LastName = value
        End Set
    End Property

    Public Property Address() As String
        Get
            Return EMP_Address
        End Get
        Set(value As String)
            EMP_Address = value
        End Set
    End Property

    Public Property Salary() As Double
        Get
            Return EMP_Salary
        End Get
        Set(value As Double)
            EMP_Salary = value
        End Set
    End Property

    Public Property Number() As Integer
        Get
            Return EMP_Number
        End Get
        Set(value As Integer)
            EMP_Number = value
        End Set
    End Property

    Public Overridable Function Payment() As Double
        Return EMP_Salary
    End Function
End Class

Public Class WeeklySalary : Inherits Employee
'The main problem "Error 2  Class 'Employee_Payment_System.Employee' has no accessible 'Sub New' and cannot be inherited." generates here

    Sub New()' I get my second error "Error 3 'Public Sub New()' has multiple definitions with identical signatures.    " here

    End Sub

    Private NumberOfWeeks As Integer

    Private Sub New() 'The second Error is related to this sub
        NumberOfWeeks = 0
    End Sub
    Public Sub New()
        NumberOfWeeks = Number
    End Sub
    Public Property Number_Of_Weeks() As Integer 'This is the number of weeks worked
        Get
            Return NumberOfWeeks
        End Get
        Set(value As Integer)
            NumberOfWeeks = value
        End Set
    End Property
    Public Overrides Function Payment() As Double

        Return NumberOfWeeks * Salary 'Calculates Weekly salary here

    End Function
End Class

I still have to code one more Class for monthly payments, and I still need to code the module for the console interface but I need my classes to work before I can start the module (For testing purposes).

Upvotes: 0

Views: 213

Answers (3)

Christiaan
Christiaan

Reputation: 669

Okay I have managed to fix it, I have changed my private constructors to public and it worked thank you

Upvotes: 1

David Wilson
David Wilson

Reputation: 4439

I'm not writing this as a criticism, just as a general pointer about inheritance.

The idea of inheritance is that child classes are the same kind of object as the parent class, but with extra functionality.

To have a real world comparison, Somebody's wages aren't the same as the somebody.

However you could have for example a class Human. A child class of that could be an employee while another child class could be a customer. Each with their own properties.

An employee could have a StartOfEmployment property while a customer could have for example a LoyaltyCardPoints property, and they could both have ContactAddress and Gender.


Anyway. To the answer - or one possible one at any rate

I suspect that you're looking at inheritance the wrong way round. The employee could possibly inherit the WeeklySalary class, while a class of Manager could possibly inherit from a MonthlySalary Class. But you'd probably be better setting up some sort of datatable using employee numbers as a key. if you do it the way youre doing at the moment, you'd have a lot of either duplicated or unused data in your program.


The naming of parent and child classes sometimes is counter intuitive as we often think as children as being smaller than the parents, but in programming, the child is the same as the parent but with extra funtionality.

Upvotes: 0

Palle Due
Palle Due

Reputation: 6292

As jonrsharpe says: Why are you making your constructors (Sub New) private? When adding a Public Sub New without parameters it clashes with Private Sub New(). You cannot have to methods with the same name and foot print. But what are you trying to achieve by inheriting from Employee? If you are trying to do some EmployeeWorkRecord class it should probably aggregate Employee and work records and not inherit from it.

Upvotes: 0

Related Questions