my assignment is to create a gui with the following code;
I have a txt box for: first name, last name, weekly salary, commission rate and gross sales.
(I will eventually have to add hourly, but i want this to work first. Line 39 (Salaried Employee) says: class 'salaried employee' must either be declared 'Must Inherit' or override the following inherited 'MustOverride' member(s):Public MustOverride Function CalculateEarnings() As Decimal...in that same class there is a blue line under MyBase.New(first, last, ssn) :Constructor call is valid only as the first statement in an instance constructor?
What does this mean? How can I fix it and do I need a txt box for SalariedEmployees? Is that what the problem is?
I have a txt box for: first name, last name, weekly salary, commission rate and gross sales.
(I will eventually have to add hourly, but i want this to work first. Line 39 (Salaried Employee) says: class 'salaried employee' must either be declared 'Must Inherit' or override the following inherited 'MustOverride' member(s):Public MustOverride Function CalculateEarnings() As Decimal...in that same class there is a blue line under MyBase.New(first, last, ssn) :Constructor call is valid only as the first statement in an instance constructor?
What does this mean? How can I fix it and do I need a txt box for SalariedEmployees? Is that what the problem is?
Public Class Form1
'Employee abstract base class
Public MustInherit Class Employee
Public Property FirstName() As String
Public Property LastName() As String
Public Property SocialSecurityNumber() As String
'three-argument constructor
Public Sub New(ByVal first As String, ByVal last As String, ByVal ssn As String)
FirstName = first
LastName = last
SocialSecurityNumber = ssn
End Sub 'New
'return String representation of Employee object
Public Overrides Function ToString() As String
Return String.Format("{0} {1}{2}{3} {4}", FirstName, LastName,
vbCrLf, "social security number:", SocialSecurityNumber)
End Function
'abstract method overridden by derived classes
Public MustOverride Function CalculateEarning() As Decimal
End Class 'Employee
Public Class SalariedEmployee
Inherits Employee
Private weeklySalaryValue As Decimal 'employee's weekly salary
'four-argument constructor
Public Sub Ne(ByVal first As String, ByVal last As String,
ByVal ssn As String, ByVal salary As Decimal)
MyBase.New(first, last, ssn) 'pass to Employee constructor
WeeklySalary = salary ' balidate and store salary
End Sub
'property WeeklySalary
Public Property WeeklySalary() As Decimal
Get
Return weeklySalaryValue
End Get
Set(ByVal salary As Decimal)
If salary >= 0D Then 'validate salary
weeklySalaryValue = salary
Else
Throw New ArgumentOutOfRangeException("Salary must be greater than or equal to 0")
End If
End Set
End Property 'weeklySalary
'calculate ernings; override abstract method CalculateEarnings
Public Overrides Function CalculateEarnings() As Decimal
Return WeeklySalary
End Function 'CalculateEarnings
'return String representation of SalariedEmployee object
Public Overrides Function ToString() As String
Return String.Format("salaried employee: {0}{1}weekly salary: {2:C}",
End Function 'ToString
End Class
'commissionEmployee Class represents a comission employee.
Public Class CommissionEmployee
Inherits Employee
Private grossSalesValue As Decimal 'gross weekly sales
Private commissionRateValue As Double 'commission percentage
'five argument constructor
Public Sub New(ByVal first As String, ByVal last As String,
ByVal ssn As String, ByVal sales As Decimal, ByVal rate As Double)
MyBase.New(first, last, ssn) 'pass to Employee constructor
GrossSales = sales 'validate and store gross sales
CommissionRate = rate 'validate and store commission rate
End Sub
'property GrossSales
Public Property GrossSales() As Decimal
Get
Return grossSalesValue
End Get
Set(ByVal sales As Decimal)
If sales >= 0D Then 'validate gross sales
grossSalesValue = sales
Else
Throw New ArgumentOutOfRangeException(
"Gross sales must be greater than or equal to 0")
End If
End Set
End Property 'GrossSales
'property CommissionRate
Public Property CommissionRate() As Double
Get
Return commissionRateValue
End Get
Set(ByVal rate As Double)
If rate > 0.0 AndAlso rate < 1.0 Then 'validate rate
commissionRateValue = rate
Else
Throw New ArgumentOutOfRangeException(
"Interest rate must be greater than 0 and less than 1")
End If
End Set
End Property 'commissionRate
'calculate earnings
Public Overrides Function calculateEarning() As Decimal
Return Convert.ToDecimal(CommissionRate) = GrossSales
End Function ' CalculateEanings
'return String representation of CommissionEmployee object
Public Overrides Function ToString() As String
Return String.Format("commission employee: {0}{1}" &
"gross sales: {2:C}{1}commission rate: {3:F}",
MyBase.ToString(), vbCrLf, GrossSales, CommissionRate)
End Function 'ToString
End Class 'CommissionEmployee
Public Class BasePlusCommissionEmployee
Inherits CommissionEmployee
Private baseSalaryValue As Decimal 'base salary per week
'six-arument constructor
Public Sub New(ByVal first As String, ByVal last As String, ByVal ssn As String, ByVal sales As Decimal,
ByVal rate As Double, ByVal salary As Decimal)
'call commissionEmployee constructor
MyBase.New(first, last, ssn, sales, rate)
BaseSalary = salary 'validate and store base salary
End Sub
'property Base Salary
Public Property BaseSalary() As Decimal
Get
Return baseSalaryValue
End Get
Set(ByVal salary As Decimal)
If salary >= 0D Then 'validate base salary
baseSalaryValue = salary
Else
Throw (New ArgumentOutOfRangeException("Base salary must be greater than or equal to 0"))
End If
End Set
End Property 'BaseSalary
'Calculate earnings
Public Function CalculateEarnings() As Decimal
Return BaseSalary + MyBase.calculateEarning()
End Function 'CalculateEarnings
'return String representation of BasePlusCommissionEmployee object
Public Overrides Function ToString() As String
Return String.Format("base-pluse-{0}{1}base salary: {2:C}"),
MyBase.ToString(), vbCrLf, BaseSalary)
End Function
End Class
'Employee hierarchy test program
Public Class PolymorphismTest
Private Sub PolymorphismTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Display each employee's info nonpolymorphically
outputTextBox1.AppendText(
"Employees processed individually:" & vbCrLf & vbCrLf &
String.Format ("{0}{1}earned: {2:C}{1}{1}",
salariedEmployee.ToString(), vbCrLf,
salariedEmployee.CalculateEarning()) &
String.Format("{0}{1} earned: {2:C}{1}{1}" &
commissionEmployee.ToString(), vbCrLf
commissionEmployee.CalculateEarning()) &
String.Format("{0}{1}earned: {2:C}",
basePlusCommissionEmployee.ToString(), vbCrLf,
basePlusCommissionEmployee.CalculateEarnings()))
'create three-element Employee array
Dim employees() As Employee = {salariedEmployee, commissionEmployee, basePlusCommissionEmployee)
outputTextBox2.AppendText(
"employees processed polymorphically:" & vbCrLf & vbCrLf)
'polymorphically process each element in array employees
For Each currentEmployee In Employee
outputTextBox2.AppendText(
String.Format("{0}{1}earned {2:C}{1}{1}",
currentEmployee.ToString(), vbCrLf,
currentEmployee.CalculateEarnings()))
Next
End Sub
End Class
End Class