Question on using "Property"

  • Thread starter Thread starter Gary
  • Start date Start date
G

Gary

Hi,

In the program below, C = A+B, and the aim is to delay the calculation
of C until the user asks for C. The boolean mUTD (UpToDate) is used
indicate when Calc() must be run to make the C = A+B calculation before
C is returned to user.

When the Driver program at bottom is run, the behavior is very strange,
the debug statement "in Calc" gets printed several times on entry to the
constructor, and then again several times when T.A = 1 is executed, and
again several times when T.B = 2 is executed (a grand total of 30 times
for the full execution of Driver. When you step through, the debugger
never actually takes you into Calc() -- you just see the "in calc nn"
printed in output window.

I am using Visual Basic.net, version 2003, Windows XP Pro on a Dell.

I am apparently not understanding something about Property?? or ??
Help!!

Public Class TestCls

Private mA As Double
Private mB As Double
Private mC As Double
Private mUTD As Boolean = False
Private Count As Integer = 0

Property A() As Double
Get
Return mA
End Get
Set(ByVal Value As Double)
mA = Value
mUTD = False
End Set
End Property

Property B() As Double
Get
Return mB
End Get
Set(ByVal Value As Double)
mB = Value
mUTD = False
End Set
End Property


ReadOnly Property C() As Double
Get
If Not mUTD Then Calc()
Return mC
End Get
End Property

Public Sub New()
mUTD = False
End Sub

Private Sub Calc()
mUTD = True
mC = mA + mB
Count = Count + 1
Debug.WriteLine("In Calc " & Count.ToString)
End Sub
End Class

----------------- Driver Program ----
Module TestDrive
Sub Main()
Dim T As New TestCls
Dim xx As Double
T.A = 1
T.B = 2
xx = T.C
End Sub
End Module

Thanks -- Gary
 
* Gary said:
In the program below, C = A+B, and the aim is to delay the calculation
of C until the user asks for C. The boolean mUTD (UpToDate) is used
indicate when Calc() must be run to make the C = A+B calculation
before C is returned to user.


When the Driver program at bottom is run, the behavior is very
strange, the debug statement "in Calc" gets printed several times on
entry to the constructor, and then again several times when T.A = 1 is
executed, and again several times when T.B = 2 is executed (a grand
total of 30 times for the full execution of Driver. When you step
through, the debugger never actually takes you into Calc() -- you just
see the "in calc nn" printed in output window.


I am using Visual Basic.net, version 2003, Windows XP Pro on a Dell.

I am apparently not understanding something about Property?? or ??
Help!!

Public Class TestCls

Private mA As Double
Private mB As Double
Private mC As Double
Private mUTD As Boolean = False
Private Count As Integer = 0

Property A() As Double
Get
Return mA
End Get
Set(ByVal Value As Double)
mA = Value
mUTD = False
End Set
End Property

Property B() As Double
Get
Return mB
End Get
Set(ByVal Value As Double)
mB = Value
mUTD = False
End Set
End Property


ReadOnly Property C() As Double
Get
If Not mUTD Then Calc()
Return mC
End Get
End Property

Public Sub New()
mUTD = False
End Sub

Private Sub Calc()
mUTD = True
mC = mA + mB
Count = Count + 1
Debug.WriteLine("In Calc " & Count.ToString)
End Sub
End Class

----------------- Driver Program ----
Module TestDrive
Sub Main()
Dim T As New TestCls
Dim xx As Double
T.A = 1
T.B = 2
xx = T.C
End Sub
End Module

I don't see any reason why your code should not work. Are you sure
there is no other code in your project and really 'Sub Main' is
executed?
 
Gary said:
Hi,

In the program below, C = A+B, and the aim is to delay the calculation
of C until the user asks for C. The boolean mUTD (UpToDate) is used
indicate when Calc() must be run to make the C = A+B calculation
before C is returned to user.

When the Driver program at bottom is run, the behavior is very
strange, the debug statement "in Calc" gets printed several times on
entry to the constructor, and then again several times when T.A = 1
is executed, and again several times when T.B = 2 is executed (a
grand total of 30 times for the full execution of Driver. When you
step through, the debugger never actually takes you into Calc() --
you just see the "in calc nn" printed in output window.

Do you see this behaviour when you just run the program all through, or when
stepping in the debugger. Because in the first case I can't reproduce it,
Calc() gets called only once when I just run the program without ever
breaking.

In the debugger the results you see might make sense if the 'C' property is
in either the "Autos", "Locals" or "Watch" window, because every time the
debugger evaluates C for display in these windows, Calc() gets called if
mUDC=False. The debugger would indeed not step into Calc() when that
happens.
 
Hi,

I did indeed have "C" displayed in the "me" window during debug, and
apparently that was the problem, since it works fine when I close the
"me" window, or just run it without debug.

Thanks -- Gary
 
Back
Top