creating an Employee class

George

Member
Joined
Jun 2, 2005
Messages
11
Programming Experience
Beginner
Hi all...
Could anyone help me with this code please ?
I need to create a base class "shopkeeper" with the following fields...

Constructor
Takes first name and last name as arguments

Properties
First name and last name

Method
To String (this overrides the base class method and returns the full name of the shopkeeper in the form

Abstract read only property
Monthly pay

This is what I have so far''''

Public Class Employee
Private strFirstName As String
Private strLastName As String

Public Sub New(ByVal FirstName As String, ByVal LastName As String)




End Sub
End Class

I know, not a lot, If you could just help me get this first bit I'm sure the rest of what I have to code will fall into place. Please, any help or pointers would be much appreciated.

Thanks for your time.
 
Last edited by a moderator:
Here you go:
VB.NET:
Public Class Employee

    Private strFirstName As String
    Private strLastName As String
    Private intMonthlyPay As Integer

    Public Sub New(ByVal FirstName As String, ByVal LastName As String)
        strFirstName = FirstName
        strLastName = LastName
    End Sub

    Public Sub New(ByVal FirstName As String, ByVal LastName As String, _
      ByVal MonthlyPay As Integer)
        Me.New(FirstName, LastName)
        intMonthlyPay = MonthlyPay
    End Sub

    Public Property FirstName() As String
        Get
            Return strFirstName
        End Get
        Set(ByVal Value As String)
            FirstName = Value
        End Set
    End Property

    'use above example for LastName property

    Public Overrides Function ToString() As String
        Return strFirstName & " " & strLastName
    End Function

    Public ReadOnly Property MonthlyPay() As Integer
        Get
            Return intMonthlyPay
        End Get
    End Property

    Public Sub SetMonthlyPay(ByVal MonthlyPay As Integer)
        intMonthlyPay = MonthlyPay
    End Sub

End Class
Regarding the monthlyPay readonly property: since it's readonly I've included an overloaded contructor (Sub New) and a function that will allow the Private variable intMonthlyPay to be set.

**Please use more descriptive titles for your posts. I've change it for this one :)
 
Last edited:
Much appreciated

Thank you so much Paszt, you have really helped me out...This will definitely get me started and I'll will get back in touch if I get stuck again if thats ok...

Thanks again for taking the time out to help me.

George
 
Further help.....

Hi Praszt,

Ok back again, first bit worked great, now what I'm not sure about is I'm asked to create 4 sub classes one being the boss class which require the following fields--Constructor--Takes first name and last name as annual salary arguments Properties--first name,last name,annual salary,monthly pay(Annual salary/12) Method--ToString (this overrides the base class method and returns the type of employee and full name of the employee in the form "Boss: John Does"
There are another 3 classes but I think I will be able to work those out by myself if I can get this first one sorted

Also once I have that sorted how will I actually get it to appear ? I have a display button on the form, I'm assuming I need to call the class somehow and write code for the button to display it all....Mmmm, not sure how to do that either....( i know, I don't know much do I)

I know I'm asking a lot, its just this thing is driving me crazy, if you could find the time to help me even further it would be much appreciated...

Thanks in advance
George
 
OK, I see now why MonthlyPay is required to be a read only property.
The contructor of the Employee class should read:
VB.NET:
Public Sub New(ByVal FirstName As String, ByVal LastName As String, _
  ByVal AnnualSalary As Integer)
    strFirstName = FirstName
    strLastName = LastName
    intAnnualSalary = AnnualSalary
End Sub
And the readonly MonthlyPay property:
VB.NET:
Public ReadOnly Property MonthlyPay() As Integer
    Get
        Return intAnnualSalary / 12
    End Get
End Property
Now once you have the Employee class correct, use Inheritance to create the other sub classes. For instance:
VB.NET:
Public Class Boss
    Inherits Employee

    Public Sub New(ByVal FirstName, ByVal LastName, ByVal AnnualSalary)
        MyBase.New(FirstName, LastName, AnnualSalary)
        Me.EmployeeType = "Boss"
    End Sub

End Class
Include a Public EmployeeType variable or property in the Employee class and incorporate the EmployeeType into the ToString method I've shown above.
Have a go and see what you come up with.
Good luck!
 
Creating a sub class

Thanks again for all the help Praszt, you're a star....!

I will give what you said a bash tonight and let you know how it went.

George
 
Hi there Praszt,

Ok, so this is what I have for my Base class....
Public Class myBaseEmployee

Private strFirstName As String

Private strLastName As String

Public intAnnualSalary As Integer

Public strEmployeeType As String



Public Sub New(ByVal FirstName As String, ByVal LastName As String, ByVal AnnualSalary As Integer)

strFirstName = FirstName

strLastName = LastName



End Sub

Public Sub New(ByVal EmployeeType As String)

strEmployeeType = EmployeeType

End Sub

Public Property EmployeeType() As String

Get

Return strEmployeeType

End Get

Set(ByVal Value As String)

strEmployeeType = Value

End Set

End Property

Public Property FirstName() As String

Get

Return strFirstName

End Get

Set(ByVal Value As String)

strFirstName = Value

End Set

End Property

Public Property LastName() As String

Get

Return strLastName

End Get

Set(ByVal Value As String)

strLastName = Value

End Set

End Property

Public Overrides Function Tostring() As String

Return EmployeeType & strFirstName & " " & strLastName

End Function

ReadOnly Property MonthlyPay() As Integer

Get

Return intAnnualSalary / 12

End Get

End Property



End
Class

And this is what I have for my base class....

Public Class Boss

Inherits myBaseEmployee

Public Sub New(ByVal FirstName, ByVal LastName, ByVal AnnualSalary, ByVal EmployeeType)

MyBase.New(FirstName, LastName, AnnualSalary)

Me.EmployeeType = "Boss"

End Sub

End
Class

Is that all right ? If so my next question to you is how do I display that in a message box once I click my display button ?

Kind regards
George
 
Back
Top