问题描述
好吧,我对VB.NET中的一件事感到惊讶
我在类中有一个属性GetUniqueID作为String,私有成员
UniqueID
此属性仅增加变量UniqueID(UniqueID + = 1)并返回新的
值。
我注意到在调试模式下,每次我悬停鼠标给那个属性打电话
以查看当前值,实际代码是执行
并且变量保持不变
这是正常的吗?
我只会想到同时执行我的财产的get block
并且返回值不能有效地改变在课堂上的价值,所以
而不是有良好的有序ID'我在每个
调试测试中都有一些更大的值
你的想法?
-
干杯,
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?
--
Cheers,
Crirus
------------------------------
If work were a good thing, the boss would take it all from you
------------------------------
推荐答案
是的。一个属性应该*总是*在被调用两次时返回相同的值 - 或者
至少它不应该因为调用
本身而返回不同的值。请改用功能。名称Get *已经表示它是
a功能。
-
Armin
http://www.netmeister.org/news/learn2quote.html
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.
--
Armin
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
这是使用/ method /的典型案例属性。
属性用于建模实体的属性。计算一个新的
ID不是属性,它是对象可以做的事情(实现为
方法)。
\\\
公共函数GetUniqueId()As Integer
UniqueID = UniqueID + 1
返回UniqueID
结束功能
///
-
Herfried K. Wagner [MVP]
< http://www.mvps.org/dotnet>
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
///
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
这篇关于调试行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!