Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to OOP and vb.net, up till now i have been coding in VBA. I am really having a hard time understanding the difference between the Class Get/Set vs. just using a public function with a private variable. What is porpoise of Get/Set. The code below is what i do when i need to get a private variable.

VB
Public Class Dog
    Private DogRunning as Boolean


    Public Function IsDogRunning()
       'Find Out if Dog is Running
       If DogIsRunning then 
            DogRunning = True
       Else
            DogRunning = False
       End If

       Return DogRunning
    End Function
End Class


Thank you in advance,

-J
Posted

I have read somewhere that before introducing properties (Get/Set) in .NET, the architects of .NET have thought whether it is necessary to introduce properties, as the similar function can be obtained from the methods. Finally the properties have been introduced in .NET.
As I understand the purpose of Get/Set (Property) is as follows:

  1. A property describes an attribute of an object. Say Color of a Car, whether the car has AC like so.
  2. The Setter and Getter blocks are cohesively encapsulated into a single member of the object i.e. the Property
    For eg.
    VB
    Public Class Car1
        Private mBodyColor As Color
        Public Property BodyColor() As Color
            Get
                'Do some work
                Return mBodyColor
            End Get
            Set(ByVal Value As Color)
                mBodyColor = Value
                'Do some validation and any other relevant work
            End Set
        End Property
    End Class
    Public Class Car4
        Private mBodyColor As Color
        Public Function GetBodyColor() As Color
            'Do some work
            Return mBodyColor
        End Function
        Private Sub SetBodyColor(ByVal value As Color)
            mBodyColor = value
            'Do some work
        End Sub
    End Class

    In the above example in Car1 class the property is used which has both Get and Set methods.
    Using methods for the same purpose we have to manually name the methods like Get... and Set... so that we can recognize them together as used in Car4 class. So, there is a chance of missing if the name is not given in the above format.
  3. With properties it is easy to designate a Property as ReadOnly or WriteOnly as below:

    VB
    Public Class Car2
        Private mBodyColor As Color
        Public ReadOnly Property BodyColor() As Color
            Get
                Return mBodyColor
            End Get
        End Property
    End Class
    Public Class Car3
        Private mBodyColor As Color
        Public WriteOnly Property BodyColor() As Color
            Set(ByVal Value As Color)
                mBodyColor = Value
            End Set
        End Property
    End Class

    Same thing can be accomplished using the methods but it is not intuitive.
  4. Where an action is to be performed like Drive car, ChangeGear etc. then a method is used and where an attribute like Color of the car, has AC etc. are required a property is used
  5. Validation and other actions can be performed in the Get and Set blocks of a property before returning a value and setting a value.
  6. In some cases the dividing line between using a Property and a Method is fine and is based on experience.


I think you may get more detailed explanation from the documentation and other references. I tried to put here what I understand

[Edit]

For the case given in the question, IsDogRunning only queries the state of the Object I think the Property as below is suitable
VB
Public Class Dog
    Private mIsDogRunning as Boolean
    Public Property IsDogRunning() as Boolean
       Get
           Return mIsDogRunning
       End Get
       Set (ByVal Value as Boolean)
           mIsDogRunning = Value
       End Set
    End Property
End Class
 
Share this answer
 
v3
Comments
Sander Rossel 19-May-12 13:26pm    
Good and detailed answer, have a 5 :)
VJ Reddy 20-May-12 0:24am    
Thank you, Naerling :)
juno101 19-May-12 19:46pm    
Thank you very much VJ Reddy, that is the answer that i was looking for!!!
VJ Reddy 20-May-12 0:25am    
You are welcome and thank you for accepting the solution :)
Properties, internally, are nothing but a pair of methods. They basically evaluate to a get and set accessor method.

You should use properties, unless the property is going to cause some unexpected, potentially long running side effect, or there is some other good reason to use a method.

For details, I suggest reading the Property Usage Guidelines on MSDN . In particular, use a method when:

The operation is a conversion, such as Object.ToString.
The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
Obtaining a property value using the get accessor would have an observable side effect.
Calling the member twice in succession produces different results.
The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order.
The member is static but returns a value that can be changed.
The member returns an array.
Otherwise, I'd use a property. Brad Abram's blogged some other details, including good reasons why certain API functions use methods (mostly because they could cause cross-computer communication, which would fall into the "side effect" category).
 
Share this answer
 
Comments
Sander Rossel 19-May-12 13:26pm    
Good answer, my 5.
juno101 19-May-12 19:48pm    
Thank you Sangramsingh Pawar, very good addition to the above answer
VJ Reddy 20-May-12 0:24am    
Good addition. 5!
Adding to what has already been said, a Property DOES create two Functions 'under the hood'.
When looking at the generated IL (Intermediate Language[^], this is the language that both VB and C# are translated into when compiled and that actually runs your application) you'll see that a Property such as the following:
VB
' Sample code from VJ Reddy's answer.
Private mBodyColor As Color
Public Property BodyColor() As Color
    Get
        'Do some work
        Return mBodyColor
    End Get
    Set(ByVal Value As Color)
        mBodyColor = Value
        'Do some validation and any other relevant work
    End Set
End Property
Generates two functions in IL, get_BodyColor and set_BodyColor.
So basically a Property is a call to a get and set function.
Why you should use a Property in .NET instead of a Public field has already been said I think. For me the most important reason is Encapsulation[^]. If, for some reason, you would have to validate values or set some other values when a field changes this can only be done through Properties or Functions. However, had you not chosen to have a Property or Function and only have a Public field then this is not possible anymore (or it would require a 'breaking change'). So why Properties instead of Functions? Properties force the programmer into an accepted and recognizable coding style where Functions do not.
 
Share this answer
 
v2
Comments
VJ Reddy 20-May-12 0:27am    
Good addition. 5!
Sander Rossel 20-May-12 5:15am    
Thank you :)
quote
VB
Public Class Car1
    Private mBodyColor As Color
    Public Property BodyColor() As Color
        Get
            'Do some work
            Return mBodyColor
        End Get
        Set(ByVal Value As Color)
            mBodyColor = Value
            'Do some validation and any other relevant work
        End Set
    End Property
End Class
 
Share this answer
 
v2
Comments
Sander Rossel 19-May-12 13:25pm    
How is simply copying the above answer a valid answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900