类构造函数中变量的命名约定
过去,当我编写类和构造函数时,我将构造函数参数中的变量命名为与实际类本身中存储的变量不同的名称。我曾经认为这会让事情变得不那么混乱,但现在我认为这实际上更令人困惑。
我现在所做的是将它们命名为相同的名称,并使用 Me.varname
引用内部变量。这是我刚刚开始构建的一个类。我的命名约定不正确吗?
Public Class BufferPosition
Public Offset As Integer
Public LoopCount As Integer
Public Sub New()
End Sub
Public Sub New(ByVal Offset As Integer, ByVal LoopCount As Integer)
Me.Offset = Offset
Me.LoopCount = LoopCount
End Sub
End Class
谢谢您的宝贵时间。
In the past when I have written classes and constructors, I named the variables in the constructor parameter something different than what would have been stored in the actual class itself. I used to think this made things less confusing, but these days I think that is actually more confusing.
What I do now is name them the same, and reference the internal variables with Me.varname
. Here is a class I just started building. Is my naming convention incorrect?
Public Class BufferPosition
Public Offset As Integer
Public LoopCount As Integer
Public Sub New()
End Sub
Public Sub New(ByVal Offset As Integer, ByVal LoopCount As Integer)
Me.Offset = Offset
Me.LoopCount = LoopCount
End Sub
End Class
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会这样做
I would do this
参考新版本(VS2013)更新上面 Fredou 的答案:
您只需要编写一行来定义属性。示例:
Visual Basic 将自动定义一个名为 _Offset 的(内部)私有变量。因此,您也不需要编写显式的 Get-Set 语句。因此,简单来说,上面的行替换了下面的所有代码:
Update on Fredou's answer above with reference to the new version (VS2013) :
You just need to write one line to define a property. Example :
Visual Basic will automatically define an (internal) private variable called _Offset. So, you also don't need to write explicit Get-Set statements. So, in simple words, the above line replaces all of the code below :