locked
Questions about getter and setter RRS feed

  • Question

  • User1080700311 posted

    I created a getter and Setter in Visual Basic. I have some questions though. Please look at the code below.
    Here are my questions.

    A.) At line 1 below, should "pieces" be declared as Private?
    B.) In the IDE at lines 4 and 7 the IDE keeps changing those variables to uppercase so that the property name "Pieces" is the
        same as the variable name "pieces".Should I instead define the integer pieces with an underscore: _pieces ?
    That is the only way I can get around the IDE forcing the variable to become uppercase.

     

    1  Dim pieces As Integer

    2  Public Property Pieces() As String
    3  Get

    4  Return pieces
    5  End Get

    6 Set (ByVal values As String)
    7   pieces=value
    8 End Set
    9 End Property

    Thursday, August 12, 2010 12:02 AM

Answers

  • User1224194097 posted

    1  Dim pieces As Integer

    Usually, it is a kind of standard to use m_ for the private variable.

    In your case as both variable name and property name are same and Thats why VS is making that change.

    I recommend using m_Pieces for the variable name


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 12:07 AM
  • User-744022866 posted

    The code you have written will not get compiled. VB is case insensitive. that means pieces and Pieces are same and you can not declare two variables with same name. So it will throw error.


    In case of c#, since it is case sensitive, pieces and Pieces are diffrent and you could compile the above(coverted to c#). So I prefer you to use _pieces for member variable and Pieces for property.


    hope this helps


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 12:49 AM
  • User1105131773 posted

     Hi

    Just a quick tip - if you type the word property and press tab this invokes a shortcut which creates a stub for you - from there you can simply just set the local member, type and property names. Quick and easy.

    We generally use an underscore to determine local member variable names, for example

        Private _myProperty As String
        Public Property MyProperty() As String
            Get
                Return _myProperty
            End Get
            Set(ByVal value As String)
                _myProperty = value
            End Set
        End Property


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 13, 2010 6:35 AM
  • User1048247244 posted

    If you are using .NET 4 you can simply declare a property.

    VB.NET 4

    Public Property MyProperty As String


    C# 3.5 and above

    public property MyProperty { get; set ;}


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 13, 2010 4:38 PM

All replies

  • User1224194097 posted

    1  Dim pieces As Integer

    Usually, it is a kind of standard to use m_ for the private variable.

    In your case as both variable name and property name are same and Thats why VS is making that change.

    I recommend using m_Pieces for the variable name


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 12:07 AM
  • User-744022866 posted

    The code you have written will not get compiled. VB is case insensitive. that means pieces and Pieces are same and you can not declare two variables with same name. So it will throw error.


    In case of c#, since it is case sensitive, pieces and Pieces are diffrent and you could compile the above(coverted to c#). So I prefer you to use _pieces for member variable and Pieces for property.


    hope this helps


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 12:49 AM
  • User1105131773 posted

     Hi

    Just a quick tip - if you type the word property and press tab this invokes a shortcut which creates a stub for you - from there you can simply just set the local member, type and property names. Quick and easy.

    We generally use an underscore to determine local member variable names, for example

        Private _myProperty As String
        Public Property MyProperty() As String
            Get
                Return _myProperty
            End Get
            Set(ByVal value As String)
                _myProperty = value
            End Set
        End Property


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 13, 2010 6:35 AM
  • User1048247244 posted

    If you are using .NET 4 you can simply declare a property.

    VB.NET 4

    Public Property MyProperty As String


    C# 3.5 and above

    public property MyProperty { get; set ;}


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 13, 2010 4:38 PM