直接访问成员变量还是通过定义它的类中的属性? [英] Access member variable directly or through property within the class it is defined?

查看:90
本文介绍了直接访问成员变量还是通过定义它的类中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



(略带宗教问题):


假设我有以下课程:


- -snip ---

Public Class MyClass

Private _MyVariable As Integer


公共属性MyVariable()As Integer

获取

返回Me._MyVariable

结束获取

设置(ByVal值为整数)

''(在这里进行一些值检查)

Me._MyVariable = value

结束集

结束财产


Private Sub MyMethod1

_MyVariable = 0

End Sub


Private Sub MyMethod2

MyVariable = 0

End Sub


结束班

---剪辑---


是否有推荐的最佳实践(类库设计

指南?)来访问_MyVariable?


MyMethod1采用直接路由,从而绕过财产制定者的任何价值检查和副作用。


MyMethod2通过属性设置器,它具有价值检查/副作用的好处,但可能是

过度杀伤,考虑到

额外的函数调用,特别是如果指定的值是

已知有效。


仍然,如果优先级是*可维护性*,仅在构造函数中使用直接访问并且在所有其他情况下使用

属性设置器不仅仅是

吗?为了避免因多次设置多个属性而产生的多方

效果

(例如,如果该类是图形控件),最好是

练习是禁用所有更新,设置多个属性,

然后再次启用更新,以便在内部使用属性设置器




你做什么?


/ JB


(Slightly religious question):

Suppose I have the following class:

---snip---
Public Class MyClass
Private _MyVariable As Integer

Public Property MyVariable() As Integer
Get
Return Me._MyVariable
End Get
Set(ByVal value As Integer)
''(some value checking going on here)
Me._MyVariable = value
End Set
End Property

Private Sub MyMethod1
_MyVariable = 0
End Sub

Private Sub MyMethod2
MyVariable = 0
End Sub

End Class
---snip---

Is there a recommended best practice (class library design
guidelines?) for accessing _MyVariable?

MyMethod1 takes the direct route, thereby bypassing any
value checking and side effects in the property setter.

MyMethod2 goes through the property setter, which has
the benefit of value checking/side effects, but might be
overkill considering the performance hit incurred by the
extra function call, especially if the values assigned are
known to be valid.

Still, if the priority is *maintainability*, wouldn''t it be best only
to use the direct access in the constructor only and the
property setter in all other cases? To avoid multiple side
effects as a result of setting multiple properties at once
(e.g. if the class is a graphical control), would the best
practice be to disable all updates, set multiple properties,
then enable updates again, in order to use property setters
all the time internally?

What do you do?

/JB

推荐答案

恕我直言,我认为一个班级内部完全没问题。设置它的

私有变量而不通过它们的访问器。这个类(你是开发人员的b $ b $)应该知道什么时候有意义,什么时候不行。对于

实例,当你想要使用

数据绑定感知类来提升PropertyChanged事件时(是的,即使是常规的ol''类支持这个)然后

你可能想要通过他们的存取器来改变属性。


应该注意的是,通过去实现性能影响几乎为0%

通过Accessors ....但是,对于Property

Accessors实际上是work,然后为了一致性和

可维护性(也许是性能)的情况..有时实现一个类似WindowsForms的''SuspendLayout'' - 修改很多属性 -

''ResumeLayout''即使在课堂上也很优雅。这是特定情况的b $ b,绝对不是规则。


简而言之......这取决于你。


PS现在,您是否应该使用PascalCase和下划线来获取私有

变量是完全不同的事情。这是一个宗教问题。 ;-)


P.P.S.在内部访问*属性访问者*时,使用我可帮助确保使用我进行
可维护性。这是一个相当常见的编码标准

(遗憾的是在VB世界中没那么多)。该规则并不真正适用于变量本身或方法......只是属性。如果你这段时间使用

,你会明白为什么它有意义。


-

-C 。 Moya
www.cmoya.com

" Joergen Bech @ post1.tele.dk>" < jbech< NOSPAMNOSPAM>在消息中写道

新闻:vo ******************************** @ 4ax.com ...
IMHO, I think it''s perfectly fine for a Class to "internally" set its
private variables without going through their Accessors. The class (you as
the developer) should know when this makes sense and when it doesn''t. For
instance, when you want PropertyChanged events to be raised with
databound-aware classes (yes, even regular ol'' classes support this) then
you''ll probably want to change properties via their Accessors.

It should be noted that there is practically 0% performance impact by going
through the Accessors.... however, for the situations where Property
Accessors actually do WORK, then for the sake of consistency and
maintainability (and maybe performance)... sometimes implementing a
WindowsForms-like ''SuspendLayout'' - Modify Lots of Properties -
''ResumeLayout'' is nice and elegant even INSIDE the class. This is
situation-specific and definately not a "rule."

In short... it''s up to you.

P.S. Now, whether you should be using PascalCase and underscores for private
variables is a totally different matter. That IS a religious question. ;-)

P.P.S. When accessing *Properties Accessors* internally it helps
maintainability to do so using "Me." This is a fairly common coding standard
(unfortunately not so much in the VB world). The rule doesn''t really apply
to the variables themselves or to Methods... just Properties. If you use
this practice for awhile, you''ll see why it makes sense.

--
-C. Moya
www.cmoya.com
"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAM> wrote in message
news:vo********************************@4ax.com...

(略有宗教问题):

假设我有以下课程:

--- snip-- -
Public Class MyClass
Private _MyVariable As Integer

公共属性MyVariable()作为整数
获取
返回Me._MyVariable
结束获取
设置(ByVal值为整数)
''(这里有一些值检查)
Me._MyVariable = value
结束集
结束属性

私人子MyMethod1
_MyVariable = 0
End Sub

Private Sub MyMethod2
MyVariable = 0
End Sub
---剪辑---

是否有建议的最佳实践(班级图书馆设计指南?)来访问_MyVariable?

MyMethod1采用直接路由,从而绕过任何<属性设置器中的值检查和副作用。

MyMethod2通过属性设置器,它具有值检查/副作用的好处,但可能是
考虑到
额外函数调用所带来的性能损失,过度杀伤,特别是如果已分配的值已知有效。

如果优先级是*可维护性*,那么仅在构造函数中使用直接访问以及在所有其他情况下使用
属性setter是最好的吗?为了避免由于一次设置多个属性而产生多方面的影响
(例如,如果该类是图形控件),最好的做法是禁用所有更新,设置多个属性,
然后再次启用更新,以便在内部一直使用属性设置器?

你做什么?

/ JB

(Slightly religious question):

Suppose I have the following class:

---snip---
Public Class MyClass
Private _MyVariable As Integer

Public Property MyVariable() As Integer
Get
Return Me._MyVariable
End Get
Set(ByVal value As Integer)
''(some value checking going on here)
Me._MyVariable = value
End Set
End Property

Private Sub MyMethod1
_MyVariable = 0
End Sub

Private Sub MyMethod2
MyVariable = 0
End Sub

End Class
---snip---

Is there a recommended best practice (class library design
guidelines?) for accessing _MyVariable?

MyMethod1 takes the direct route, thereby bypassing any
value checking and side effects in the property setter.

MyMethod2 goes through the property setter, which has
the benefit of value checking/side effects, but might be
overkill considering the performance hit incurred by the
extra function call, especially if the values assigned are
known to be valid.

Still, if the priority is *maintainability*, wouldn''t it be best only
to use the direct access in the constructor only and the
property setter in all other cases? To avoid multiple side
effects as a result of setting multiple properties at once
(e.g. if the class is a graphical control), would the best
practice be to disable all updates, set multiple properties,
then enable updates again, in order to use property setters
all the time internally?

What do you do?

/JB



Joergen,


也许唯一的问题是你在2005版本遇到麻烦

因为变量的下划线开头并不总是符合CLS




查看此新闻组中的当前长线程来自遇到麻烦的人

。我只是略微触摸它。


实际上你的方法1和2没有意义。因此我绝对不会实施它们。


Cor
Joergen,

Maybe can the only problem be that you come in trouble in version 2005
because the start with an underscore for a variable is not always CLS
compliant.

See a long current thread in this newsgroup from someone who was in trouble
with that. I have only touched it slightly.

In fact are your methods 1 and 2 without sense. And therefore I would
absolute not implement them at all.

Cor


" Cor Ligthert [MVP]" <无************ @ planet.nl> schrieb:
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
也许唯一的问题是你在2005版本遇到麻烦
因为变量的下划线并不总是符合CLS


在这个新闻组中看到一个当前很长的线程来自那个遇到麻烦的人。我只是略微触摸它。
Maybe can the only problem be that you come in trouble in version 2005
because the start with an underscore for a variable is not always CLS
compliant.

See a long current thread in this newsgroup from someone who was in
trouble with that. I have only touched it slightly.




嗯,但这不适用于私有变量。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http ://classicvb.org/petition/>



Well, but this does not apply to private variables.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


这篇关于直接访问成员变量还是通过定义它的类中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆