Inheritance & Access Modifiers

  • Thread starter Thread starter sougata12
  • Start date Start date
S

sougata12

Guest
Const BonusRate As Decimal = 1.45D
Const PayRate As Decimal = 14.75D

Class Payroll
Overridable Function PayEmployee(
ByVal HoursWorked As Decimal,
ByVal PayRate As Decimal) As Decimal

PayEmployee = HoursWorked * PayRate
End Function
End Class

Class BonusPayroll
Inherits Payroll
Overrides Function PayEmployee(
ByVal HoursWorked As Decimal,
ByVal PayRate As Decimal) As Decimal

' The following code calls the original method in the base
' class, and then modifies the returned value.
PayEmployee = MyBase.PayEmployee(HoursWorked, PayRate) * BonusRate
End Function
End Class

Sub RunPayroll()
Dim PayrollItem As Payroll = New Payroll
Dim BonusPayrollItem As New BonusPayroll
Dim HoursWorked As Decimal = 40

MsgBox("Normal pay is: " &
PayrollItem.PayEmployee(HoursWorked, PayRate))
MsgBox("Pay with bonus is: " &
BonusPayrollItem.PayEmployee(HoursWorked, PayRate))
End Sub

came across this code in the following MSDN page:

Inheritance Basics (Visual Basic)

I am writing the above code in the code editor in the following way:

Public Class Payroll

Public hours_worked As Decimal
Const normal_rate As Decimal = 1.45
Const bonus_rate As Decimal = 1.75

Overridable Function Pay_Employee(ByVal hours_worked As Decimal,
ByVal normal_rate As Decimal) As Decimal

Pay_Employee = hours_worked * normal_rate

End Function

End Class

Public Class Bonus_Payroll
Inherits Payroll

Overrides Function Pay_Employee(ByVal hours_worked As Decimal,
ByVal normal_rate As Decimal) As Decimal

Pay_Employee = MyBase.Pay_Employee(hours_worked, normal_rate) * bonus_rate

End Function



End Class

Question:
My question is only about the line: Const bonus_rate As Decimal = 1.75

The problem is I am not able to access "BONUS_RATE" from the derived class (see picture later which shows the red swiggly line ) as it has been declared as CONST and hence deemed to be private. If I change it to public then it works fine. So my question is:
1. Is the code in msdn example wrong? Should it be PUBLIC there also?
2. Or is the location of the Const statement incorrect in the code I have written? If that is the case please tell me what should be its location...for example I tried it in the following ways but even then the inherited class was not able to access it.

1344823.png


Sougata Ghosh

Continue reading...
 
Back
Top