By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

Public properties vs. public variables

P: n/a
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"?

--
Regards,
Webster
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"Webster" <we************@home.com> wrote in message
news:eL****************@TK2MSFTNGP10.phx.gbl...
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"?

--
Regards,
Webster

Nov 20 '05 #2

P: n/a
In article <eL**************@TK2MSFTNGP10.phx.gbl>, Webster wrote:
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.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 20 Days, 11 Hours, 2 Minutes, 35 Seconds
Nov 20 '05 #3

P: n/a
* "Webster" <we************@home.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

P: n/a
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

"Webster" <we************@home.com> wrote in message
news:eL****************@TK2MSFTNGP10.phx.gbl...
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"?

--
Regards,
Webster

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.