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 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
> 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.
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
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
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
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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
...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - How do I access a property of an object using
a string?...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |