none
Inheritance & Access Modifiers RRS feed

  • Question

  • 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:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/inheritance-basics

    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. 


    Sougata Ghosh

    Sunday, October 7, 2018 12:23 PM

Answers

  • Try this

    Public Class Payroll
    
        Public hours_worked As Decimal
        Const normal_rate As Decimal = 1.45D
        Public Const bonus_rate As Decimal = 1.75D
    
        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
    Public Class Payroll
    
        Public hours_worked As Decimal
        Const normal_rate As Decimal = 1.45D
        Protected Const bonus_rate As Decimal = 1.75D
    
        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



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Sunday, October 7, 2018 12:49 PM
    Moderator