最佳实践 - 编码公约问题 [英] Best Practices - Coding Convention Question
问题描述
大家好,
我在vb.net上还是比较新的,并且想知道正确的方式来访问
私有的varibables in一类。我是直接访问变量还是使用
公共财产?
公共类MyClass
private _variableName as integer
公共属性VariableName为整数
get
return _variableName
end get
设置(byVal值为整数)
_variableName = value
结束集
结束财产
public sub DoSomething()
''我使用_variableName还是
''我是否使用VariableName"
end sub
结束班
谢谢
Ren写道:< blockquote class =post_quotes>大家好,
我仍然是vb.net的新手,想知道在类中访问私有varibables的正确方法。我是直接访问变量还是使用
公共属性?
公共类MyClass
private _variableName为整数
公共属性VariableName为整数
获取
返回_variableName
结束获取
设置(byVal值为整数)
_variableName = value
结束设置
结束属性>
public sub DoSomething()
''我使用_variableName还是
''我使用VariableName"
结束子
结束类
谢谢
不确定这方面的最佳做法,但从实践层面来说,如果你使用
财产它允许你在
未来的get / set期间进行更改,而不必重新编写其余的课程。
Chris
Ren,
我用于私人变量
mVariableName
对于你用_variableName写的其余部分我写的符号讨厌。
顺便说一下,从来没有最佳实践。幸运的是,这一直是
讨论的一部分。
VBNet开发者的典型特征是他/她是人类,可以在
$时更改b $ b它是必需的,并采取最佳实践。
Cor
" Ren" <无**** @ nospam.org> schreef in bericht
新闻:pf ******************************** @ 4ax.com ...大家好,
我仍然是vb.net的新手,想知道正确的访问方式/ >类中的私有varibables。我是直接访问变量还是使用
公共财产?
公共类MyClass
private _variableName为整数
公开属性VariableName为整数
获取
返回_variableName
结束获取
设置(byVal值为整数)
_variableName = value
结束集
结束属性
public sub DoSomething()
''我使用_variableName还是
''我是否使用VariableName"
end sub
最终班级
谢谢
" Ren" <无**** @ nospam.org> schrieb:我在vb.net上仍然很新,想知道在类中访问私有varibables的正确方法。我是直接访问变量还是使用
公共财产?
我考虑通过属性''''访问它获得''/''设置''访问器甚至
在课堂内可能的良好做法,因为它会阻止
为属性分配无效值。执行开销很小
因为JIT编译器可以内联属性访问。
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://classicvb.org/petition/>
Hi All,
I''m still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?
public class MyClass
private _variableName as integer
public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property
public sub DoSomething ()
'' do I use _variableName or
'' do I use VariableName"
end sub
end class
Thanks
Ren wrote:Hi All,
I''m still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?
public class MyClass
private _variableName as integer
public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property
public sub DoSomething ()
'' do I use _variableName or
'' do I use VariableName"
end sub
end class
Thanks
Not sure of best practice on this, but from a practice level, if you use
the property it allows you to make changes during the get/set in the
future w/o having to recode rest of the class.
Chris
Ren,
I use for the private variable
mVariableName
For the rest as you wrote with _variableName a notation that I hate.
By the way there is never a Best Practice. Luckily this is always a part of
discussion.
Typical for the VBNet developper is that he/she is human and can change when
it is needed and take the best Practice for that situation.
Cor
"Ren" <no****@nospam.org> schreef in bericht
news:pf********************************@4ax.com...Hi All,
I''m still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?
public class MyClass
private _variableName as integer
public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property
public sub DoSomething ()
'' do I use _variableName or
'' do I use VariableName"
end sub
end class
Thanks
"Ren" <no****@nospam.org> schrieb:I''m still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?
I consider accessing it through the property''s ''Get''/''Set'' accessor even
inside the class whereever possible good practice, because it will prevent
assignment of invalid values to the property. Execution overhead is minimal
because the JIT compiler can inline property access.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
这篇关于最佳实践 - 编码公约问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!