类构造函数中变量的命名约定

发布于 2024-10-04 09:52:03 字数 501 浏览 2 评论 0原文

过去,当我编写类和构造函数时,我将构造函数参数中的变量命名为与实际类本身中存储的变量不同的名称。我曾经认为这会让事情变得不那么混乱,但现在我认为这实际上更令人困惑。

我现在所做的是将它们命名为相同的名称,并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尽揽少女心 2024-10-11 09:52:03

我会这样做

Public Class BufferPosition

Private _Offset As Integer
Private _LoopCount As Integer

Public Sub New()

End Sub

Public Sub New(ByVal Offset As Integer, ByVal LoopCount As Integer)
    _Offset = Offset
    _LoopCount = LoopCount
End Sub

Public Property Offset() As Integer
    Get
        Return _Offset
    End Get
    Set(ByVal value As Integer)
        _Offset = value
    End Set
End Property

Public Property LoopCount() As Integer
    Get
        Return _LoopCount
    End Get
    Set(ByVal value As Integer)
        _LoopCount = value
    End Set
End Property

End Class

I would do this

Public Class BufferPosition

Private _Offset As Integer
Private _LoopCount As Integer

Public Sub New()

End Sub

Public Sub New(ByVal Offset As Integer, ByVal LoopCount As Integer)
    _Offset = Offset
    _LoopCount = LoopCount
End Sub

Public Property Offset() As Integer
    Get
        Return _Offset
    End Get
    Set(ByVal value As Integer)
        _Offset = value
    End Set
End Property

Public Property LoopCount() As Integer
    Get
        Return _LoopCount
    End Get
    Set(ByVal value As Integer)
        _LoopCount = value
    End Set
End Property

End Class
长发绾君心 2024-10-11 09:52:03

参考新版本(VS2013)更新上面 Fredou 的答案:

您只需要编写一行来定义属性。示例:

Public Property Name As String

Visual Basic 将自动定义一个名为 _Offset 的(内部)私有变量。因此,您也不需要编写显式的 Get-Set 语句。因此,简单来说,上面的行替换了下面的所有代码:

Public Property Name As String
    Get
        Return _Name
    End Get
    Set(ByVal value As String)
        _Name= value
    End Set
End Property
Private _Name As String

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 :

Public Property Name As String

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 :

Public Property Name As String
    Get
        Return _Name
    End Get
    Set(ByVal value As String)
        _Name= value
    End Set
End Property
Private _Name As String
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文