开发者

Naming convention for variables in class constructor

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 Intege开发者_Go百科r)
        Me.Offset = Offset
        Me.LoopCount = LoopCount

    End Sub
End Class

Thank you for your time.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜