LRoyster
LRoyster

Reputation: 53

VB.Net override a property

I need to override this property so that I can return a calculated value. I am not throwing any errors but it is not working. What am I doing wrong? Any help would be great.

This is in the base class. BalanceValue is a private decimal.

Public Overridable Property Balance() As Decimal

    Get
        Return balanceValue
    End Get

    Set(balance As Decimal)
        If balance >= 0D Then
            balanceValue = balance
        Else
            Throw New ArgumentOutOfRangeException("Balance must be greater than or equal to 0")
        End If
    End Set

End Property

This is in the derived class.

Public Overrides Property Balance() As Decimal
    Get
        Return CDec(BalanceWithInterest)
    End Get

    Set(balance As Decimal)
        MyBase.Balance = CDec(BalanceWithInterest)
    End Set
End Property

This is what I want it to return

 Public Function CalculateInterest(interestRate As Double) As Decimal

    Return CDec(MyBase.Balance + (MyBase.Balance * interestRate))

End Function

Upvotes: 1

Views: 6312

Answers (1)

Fuzhou Hu
Fuzhou Hu

Reputation: 538

I'm not totally sure what you're looking for, but maybe try this code. I'm using the Shadows instead of Overrides keyword because the parameters of the property are different in the derived class than the parent class.

  Public Shadows Property Balance(interestRate As Double) As Decimal
    Get
      Return CDec(MyBase.Balance + (MyBase.Balance * interestRate))
    End Get
    Set(balance As Decimal)
      MyBase.Balance = CDec(balance / (1 + interestRate))
    End Set
  End Property

Upvotes: 3

Related Questions