473,406 Members | 2,369 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,406 software developers and data experts.

Property or get/set methods ?

Hi,

I'm just wondering what are the guidelines for using a Property or a pair of
get/set methods related to a member variable ?
What do yu guys do ?

Thank you

Herve
Nov 16 '05 #1
8 6565

"Herve Bocuse" <gd******@gmail.com> wrote in message
news:cp**********@dns3.cae.ca...
Hi,

I'm just wondering what are the guidelines for using a Property or a pair
of
get/set methods related to a member variable ?
What do yu guys do ?


Never getX setX. Either properties or public member variables. I know many
people disagree, but I use public member variables alot on stuff that is not
visible across solutions. Public member variables should be named just like
properties, so if you later encapsulate the member variable, client code
won't break (although it will need to be recompiled). My rule of thumb for
public member variables is that they are OK instead of properties so long as
all the client classes get recompiled whenever the class containing the
member variable does.

I use initial-caps camel casing for all public data members, and
initial-lower camel casing for all private data members.

I also name constructor arguments after the public data member they
represent (possibly using this. in the constructor body to distinguish the
argument from the member).
class Foo1
{
public Foo1(int MyInt)
{
this.MyInt = MyInt;
}
public int MyInt;
}

or

class Foo2
{
public Foo2(int MyInt)
{
myInt = MyInt;
}
int myInt;
int MyInt
{
get
{
return myInt;
}
set
{
myInt = value;
}
}
}

David
Nov 16 '05 #2
> Public member variables should be named just like
properties, so if you later encapsulate the member variable, client code
won't break


For most cases, that is true; however, client code could still break -
fields can be passed ref/out while properties cannot.

Using fields instead of properties also changes the behavior of things like
the VS designer that uses GetProperties to populate the property grid.

There was another thread in this forum recently that went through a bunch of
these issues.
Nov 16 '05 #3
I wouldn't say that you should never GetX or SetX. If the setting or
getting of the property is a significant event (it would cause a long time
to execute, sets much more than just the value), then it is more appropriate
to have that in a method, then just a property.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uM**************@TK2MSFTNGP11.phx.gbl...

"Herve Bocuse" <gd******@gmail.com> wrote in message
news:cp**********@dns3.cae.ca...
Hi,

I'm just wondering what are the guidelines for using a Property or a pair
of
get/set methods related to a member variable ?
What do yu guys do ?


Never getX setX. Either properties or public member variables. I know
many people disagree, but I use public member variables alot on stuff that
is not visible across solutions. Public member variables should be named
just like properties, so if you later encapsulate the member variable,
client code won't break (although it will need to be recompiled). My rule
of thumb for public member variables is that they are OK instead of
properties so long as all the client classes get recompiled whenever the
class containing the member variable does.

I use initial-caps camel casing for all public data members, and
initial-lower camel casing for all private data members.

I also name constructor arguments after the public data member they
represent (possibly using this. in the constructor body to distinguish the
argument from the member).
class Foo1
{
public Foo1(int MyInt)
{
this.MyInt = MyInt;
}
public int MyInt;
}

or

class Foo2
{
public Foo2(int MyInt)
{
myInt = MyInt;
}
int myInt;
int MyInt
{
get
{
return myInt;
}
set
{
myInt = value;
}
}
}

David

Nov 16 '05 #4
Also, if you have a case where you want to be able to set, but not get, then
personally I think a write-only property is weird and confusing, and I would
make that a set method.

-Rachel

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eD**************@tk2msftngp13.phx.gbl...
I wouldn't say that you should never GetX or SetX. If the setting or
getting of the property is a significant event (it would cause a long time
to execute, sets much more than just the value), then it is more
appropriate to have that in a method, then just a property.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uM**************@TK2MSFTNGP11.phx.gbl...

"Herve Bocuse" <gd******@gmail.com> wrote in message
news:cp**********@dns3.cae.ca...
Hi,

I'm just wondering what are the guidelines for using a Property or a
pair of
get/set methods related to a member variable ?
What do yu guys do ?


Never getX setX. Either properties or public member variables. I know
many people disagree, but I use public member variables alot on stuff
that is not visible across solutions. Public member variables should be
named just like properties, so if you later encapsulate the member
variable, client code won't break (although it will need to be
recompiled). My rule of thumb for public member variables is that they
are OK instead of properties so long as all the client classes get
recompiled whenever the class containing the member variable does.

I use initial-caps camel casing for all public data members, and
initial-lower camel casing for all private data members.

I also name constructor arguments after the public data member they
represent (possibly using this. in the constructor body to distinguish
the argument from the member).
class Foo1
{
public Foo1(int MyInt)
{
this.MyInt = MyInt;
}
public int MyInt;
}

or

class Foo2
{
public Foo2(int MyInt)
{
myInt = MyInt;
}
int myInt;
int MyInt
{
get
{
return myInt;
}
set
{
myInt = value;
}
}
}

David


Nov 16 '05 #5
I don't expose public class variables at all. It's a bad idea, and
breaks encapsulation. It's easy to take a ref from a public class
variable and turn it into something that it shouldn't be. Properties
make sure that your vars are yours.

Personally, I believe that exposing public variables is just a sign of
laziness.

As for getX/setX methods, it depends on your taste. Technically
speaking, the properties in C# and VB.NET are replaced by the compiler
with get/set methods. To me, it's just too java-ish, but then again, I
have no problem with write-only properties. :-P
Nov 16 '05 #6

"Mike Newton" <MN*****@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I don't expose public class variables at all. It's a bad idea, and breaks
encapsulation. It's easy to take a ref from a public class variable and
turn it into something that it shouldn't be. Properties make sure that
your vars are yours.

Personally, I believe that exposing public variables is just a sign of
laziness.


Yes. I am lazy. And I agree that exposing public variables is just a sign
of laziness.

But laziness in programming is a vice in inverse proportion to the type
safety of the language.

For POD types, private types, and "friend" types, the cost of variable
encapsulation is not always worth the effort.

David
Nov 16 '05 #7

"Rachel Suddeth" <ra****@bldhound.com> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
Also, if you have a case where you want to be able to set, but not get,
then personally I think a write-only property is weird and confusing, and
I would make that a set method.


Agree on both. Properties should only be used as a replacement for a
getter, or a getter/setter pair, where the methods are:
-Not resource intensive
-The getter does not change the state of the object in any visible way
-The setter does not change the visible state of the object, except for the
value returned by the getter.
I don't like properties at all where you have to set PropertyX before you
can run MethodY. This should be a constructor argument or an argument to
MethodY.

David
Nov 16 '05 #8
A property should be used when a value is likely to change over a long
period of time. For instance, Rate Of Interest. It should be used when
your class depends upon an user input again for instance, the account
class which may depend upon the rate of interest supplied by the user
and hence, you should take it into a property.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Laszlo Zsolt Nagy | last post by:
My problem is about properties and the virtuality of the methods. I would like to create a property whose get and set methods are virtual. I had the same problems in Delphi before and the solution...
18
by: Robin Becker | last post by:
Is there a way to override a data property in the instance? Do I need to create another class with the property changed? -- Robin Becker
15
by: Prachi Dimble | last post by:
Hi, In vb.Net one can pass arguments to properties. How does one achieve it in c#? Given below is the vb.net code for passing arguments to property getters and setters.. Thanks, Prachi ...
7
by: TJ | last post by:
In C# how do you achieve pass-by-reference property declarations in the Type Library? I am writing a COM Class Library that must mimick an existing library for which the only information is the...
13
by: Peter Kirk | last post by:
Hi there, can someone tell me what exactly a "property" is in a C# class? As far as I can see it is "two methods" - ie a getter and a setter for an instance variable. What is the difference...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
6
by: David Hearn | last post by:
I have a property in a user control that I am setting: Private strPageName as String Public Property PageName() as String Get Return strPageName End Get Set(byVal Value as String)...
14
by: Dom | last post by:
Hi all I'm developing a control, and I need to hide some properties to the user. For example, suppose I need Text property to be completely inacessible (from a Form/Code that is into another...
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
7
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I access a property of an object using a string?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.