How to use values ​​from child class in parent class? vb.net

I have a problem that disappoints me to the end, I have an overridden function in the parent class and an override function in the child class, like below:

subclass

Public Overrides Sub UpdatePrice(ByVal dblRetailPrice As Double)
    If dblWholesalePrice < 0 Then
        MessageBox.Show("Error, amount must be greater than 0.")
    Else
        dblRetailPrice = dblWholesalePrice * dblStandardMargin
    End If
End Sub

      

and in the parent class, I

Public ReadOnly Property RetailPrice() As Double
    Get
        Return dblRetailPrice
    End Get
End Property

Public Overridable Sub UpdatePrice(ByVal dblRetailPrice As Double)
    If dblWholesalePrice < 0 Then
        MessageBox.Show("Please input an amount greater than 0,wholesale price has not changed", "error")
    Else
        dblRetailPrice = 1.1 * dblWholesalePrice
    End If
End Sub

      

When I debug the value is generated, but it does not carry over to the parent class ski.RetailPrice (), which seems to be the problem here? Any help would be greatly appreciated.

+3


source to share


1 answer


You should not pass a parameter with the same name as a class level variable in a higher scope. A local variable will override another, which means that this statement in your setter:

dblRetailPrice = 1.1 * dblWholesalePrice

      

will set the value of the temporary parameter dblRetailPrice

you just passed, not a member variable dblWholesalePrice

at the class level.



A simple solution is to change the parameter name by dropping the useless type notation prefix:

Public Class MyClass

    Protected dblRetailPrice As Double

    Public ReadOnly Property RetailPrice() As Double
        Get
           Return dblRetailPrice
        End Get
    End Property

    Public Overridable Sub UpdatePrice(ByVal retailPrice As Double)

       If dblWholesalePrice < 0 Then
           MessageBox.Show("Please input an amount greater than 0,wholesale price has not changed", "error")
       Else
           dblRetailPrice = 1.1 * dblWholesalePrice
       End If
    End Sub

End Class

      

+2


source







All Articles