VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
strange behavior with property procedures Posted by westfalr on 13 Nov 2008 at 9:36 PM
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




Report
Re: strange behavior with property procedures Posted by SharbellMouess on 14 Nov 2008 at 5:01 AM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.