473,383 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Public properties vs. public variables

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
4 2048
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
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
* "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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jose Meireles | last post by:
Hi everyone, I'm trying to use public variables in a web form to hld specific values. What happens is that the public variables (declared as public x as y in the beginning of the class), doesn't...
18
by: Janaka | last post by:
I'm having a discussion with my colleagues here on good programming standards. One thing we haven't agreed on is the use of properties in classes vs using member variables. Now everyone knows...
6
by: darrel | last post by:
I'm still not quite sure how best to handle the passing of data between controls. This is a method I'm using at the moment: I have an XML file that contains a variety of page-centric...
4
by: Nick Dreyer | last post by:
Is it possible to see public class variables of a COM addin in Excel 97 VBA? I have successfully created the (Visual Basic 2003 .NET) COM and referenced it in an Excel 97 VBA project. The VBA...
9
by: Stefan De Schepper | last post by:
Should I use: Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value
27
by: thomasp | last post by:
Variables that I would like to make available to all forms and modules in my program, where should I declare them? At the momment I just created a module and have them all declared public there. ...
2
by: Darrel | last post by:
I'm working on an app where the ASPX pages aren't precompiled with the class.vb files I'm. This is so people can add their own ASPX pages down the road to the app (the .aspx pages become...
6
by: t f | last post by:
Hi I have a class with a bunch of private variables in, is there an easy way to make these have public properties without having to type it all in? e.g. public class Fu { private float...
11
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.