Public properties vs. public variables

W

Webster

Hello,

Just wondering what's better programming style; to use public variables in a
class or to use private/protected variables and then expose them via
properties?


For example:
--------------------------------
Public Class public_person
Public firstname As String
Public lastname As String
End Class

---------------------------------
Public Class property_person
Dim firstname As String
Dim lastname As String

Public Property first_name() As String
Get
Return Me.firstname
End Get
Set(ByVal Value As String)
Me.firstname = Value
End Set
End Property

Public Property last_name() As String
Get
Return Me.lastname
End Get
Set(ByVal Value As String)
Me.lastname = Value
End Set
End Property
End Class


Which of the two classes is "better"?
 
S

solex

Webster,

It all depends on your requirements. If you need to control access to the
variables or set up the state of the object then definately use the accessor
"property" methods otherwise do not.

Example, say you want to determine if your object is dirty. Then you would
write something like this:

Public Class Person
Private mflgDirty as Boolean = False
Private mstrFirstname As String

Public Property FirstName As String
Get FirstName
Return mstrFirstname
End Get

Set FirstName(ByVal value As String)
If value <> mstrFirstname Then
mflgDirty = True
mstrFirstname = value
End If
End Set
End Property
End Class
 
T

Tom Shelton

Hello,

Just wondering what's better programming style; to use public variables in a
class or to use private/protected variables and then expose them via
properties?


For example:
--------------------------------
Public Class public_person
Public firstname As String
Public lastname As String
End Class

---------------------------------
Public Class property_person
Dim firstname As String
Dim lastname As String

Public Property first_name() As String
Get
Return Me.firstname
End Get
Set(ByVal Value As String)
Me.firstname = Value
End Set
End Property

Public Property last_name() As String
Get
Return Me.lastname
End Get
Set(ByVal Value As String)
Me.lastname = Value
End Set
End Property
End Class


Which of the two classes is "better"?

It is my opinion that one should never expose private data - period.
The main reason for using a property over directly accessing a type, is
that it implements your clients from implementation changes. It is
concievable that the variable you expose as a public memeber today, may
need to be accessed differently in the future. If it is wrapped in a
property you are free to do this without breaking existing clients.
 
H

Herfried K. Wagner [MVP]

* "Webster said:
Just wondering what's better programming style; to use public variables in a
class or to use private/protected variables and then expose them via
properties?

Properties.
 
J

Jay B. Harlow [MVP - Outlook]

Webster,
In addition to the other comments, I also use Properties.

Remember Encapsulation is one of the top 3 tenants of OOP. The other 2 being
Inheritance and Polymorphism.

Properties are .NET method of encapsulating field or attribute data.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top