Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

strange behavior with property procedures

westfalrwestfalr Posts: 1Member
I have some code that is deliberately programmed in a non-standard way, so I'm not looking for someone to point that out. When it's coded right, I get the proper results. However the results are not what I thought they would be with the non-standard code.

I spent some time walking through the code with the debugger. Everything was working right until just before the end, when the correct value that should have been returned changed to minus 1. Purely as a matter of curiosity, I'd like to know what's going on with the code to produce the strange result. Here it is:

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cls As New Class1
cls.Area = 8.34D
cls.Price = 31.22D
MsgBox(Format(cls.TotalCost, "Fixed"))
End Sub
End Class

Public Class Class1
Private pricee As Decimal
Private areea As Decimal
Private totCost As Decimal

Public ReadOnly Property TotalCost() As Decimal
Get
Return totCost = Area * Price
End Get
End Property

Public Property Area() As Decimal
Get
Return areea
End Get
Set(ByVal value As Decimal)
If value < 1000 Then
areea = value
Else
areea = 0
End If
totCost = value * Price
End Set
End Property

Public Property Price() As Decimal
Get
Return pricee
End Get
Set(ByVal value As Decimal)
pricee = value
totCost = value * Area
End Set

End Property

End Class




Comments

  • SharbellMouessSharbellMouess Posts: 7Member
    Hi,
    The problem lies in the TotalCost() property. You are returning a boolean expression and not the value of totCost, that's because you wrote 'Return totCost = Area * Price' which is equal to 'Return True'. The solusion is to assign a value to the variable totCost first and then return it, so your property should look like this:

    Public ReadOnly Property TotalCost() As Decimal
    Get
    totCost = Area * Price
    Return totCost
    End Get
    End Property

    Good luck


    Sharbell K. Mouess
Sign In or Register to comment.