Debug behavior

  • Thread starter Thread starter Crirus
  • Start date Start date
C

Crirus

Well, I' surprised by a thing in VB.NET
I have a property GetUniqueID as String in a class and a private member
UniqueID
This property only increment variable UniqueID (UniqueID+=1) and return new
value.
What I noticed is that in debug mode, every time I hover the mouse to a call
to that property in order to see the curent value, actualy the code is
executed and the variable remain changed

This is normal?

I whould espected only to simulte the execution of get block of my property
and return the value not effectively change the value in the class, so
instead having nice ordered ID's I have some much bigger values at every
debug test

What you think?
 
Crirus said:
Well, I' surprised by a thing in VB.NET
I have a property GetUniqueID as String in a class and a private
member UniqueID
This property only increment variable UniqueID (UniqueID+=1) and
return new value.
What I noticed is that in debug mode, every time I hover the mouse to
a call to that property in order to see the curent value, actualy the
code is executed and the variable remain changed

This is normal?

Yes. A property should *always* return the same value when called twice - or
at least it should not return a different value just because of the call
itself. Use a function instead. The name "Get*" already expresses that it is
a function.
 
* "Crirus said:
Well, I' surprised by a thing in VB.NET
I have a property GetUniqueID as String in a class and a private member
UniqueID
This property only increment variable UniqueID (UniqueID+=1) and return new
value.
What I noticed is that in debug mode, every time I hover the mouse to a call
to that property in order to see the curent value, actualy the code is
executed and the variable remain changed

That's a typical case for using a /method/ instead of a property.
Properties are used to model attributes of an entity. Calculating a new
ID is not an attribute, it's a thing the object can do (implemented as a
method).

\\\
Public Function GetUniqueId() As Integer
UniqueID = UniqueID + 1
Return UniqueID
End Function
///
 
OK, I can work around that, but is normal?
I think not, a debug call as a warch shouldnt execute anything in the same
scope as programm itself, right?
 
Crirus said:
OK, I can work around that, but is normal?
Yes

I think not, a debug call as a warch shouldnt execute anything in the
same scope as programm itself, right?

It must execute the code. How else should it get the property value?
 
Crirus said:
I was thinking on executing but not modifying the data


Executing what? I thought _you_ are modifying the data in the property get
procedure.
 
No what I meant was kind of "simulating" the code execution in order to give
me the correct value, but without retaining in the real code the value
returned, so each time I hover the mouse on a property call, to see the same
value as the excution thread will get first time
 
Crirus said:
No what I meant was kind of "simulating" the code execution in order
to give me the correct value, but without retaining in the real code
the value returned, so each time I hover the mouse on a property
call, to see the same value as the excution thread will get first
time

Well, it doesn't work this way (I can't imagine how it could). To get the
property value the property-get procedure is called. Sorry, there's nothing
more I can say.
 
Yes but any watch will comnpromise the real value that should be returned by
the property as if I dont wach it :(
 
Crirus said:
Yes but any watch will comnpromise the real value that should be
returned by the property as if I dont wach it :(

It is your mistake that you change the value. As already mentioned, you must
use a function instead.
 
Back
Top