473,804 Members | 3,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6587

"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.co m

"David Browne" <davidbaxterbro wne no potted me**@hotmail.co m> wrote in
message news:uM******** ******@TK2MSFTN GP11.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.c om> wrote in
message news:eD******** ******@tk2msftn gp13.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.co m

"David Browne" <davidbaxterbro wne no potted me**@hotmail.co m> wrote in
message news:uM******** ******@TK2MSFTN GP11.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*****@discus sions.microsoft .com> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
I don't expose public class variables at all. It's a bad idea, and breaks
encapsulatio n. 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****@bldhoun d.com> wrote in message
news:uG******** ******@TK2MSFTN GP09.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.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- 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.Ravichandr an"
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
1623
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 was the same. I created a private _get method and a public get method. The former one will call the latter. The same stands for _set and set. Here is an example below: class C1(object): def namechanged(self): """Called after the name of the...
18
4765
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
8416
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 Public Property Field(ByVal strFieldName As String, Optional ByVal tableName As String = "", Optional ByVal rowNum As Integer = 0) Get If tableName <> "" Then Return objDataSet.Tables(tableName).Rows(rowNum)(strFieldName)
7
3901
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 TypeLib. I'm using Visual Studio .NET 2003. The original library provides simple authentication services, from Access and MS-SQL OLEDB providers. The enhancement I'm creating provides support for ODBC and will be a drop-in replacement.
13
1814
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 between these: public string InstanceStringVariable() {
2
2211
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 i'm using the treenodes and each treenode needs a unique entry into my rich text box. After sitting at home with MSDN i've managed to get this functionality by storing a RTF string in the tag property of the treenode. On every 'before update' of the...
6
1525
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) strPageName = Value End Set
14
2432
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 project/assembly). I tried with attributes: <Browsable(False), _ EditorBrowsable(EditorBrowsableState.Never), _ RefreshProperties(RefreshProperties.Repaint), _
2
1682
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 dim myPlaceBets() As AU.exchange.PlaceBets many statements
7
402
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I access a property of an object using a string? ----------------------------------------------------------------------- There are two ways to access properties: the dot notation and the square bracket notation. What you are looking for is the square bracket notation in which the dot, and the identifier to its right, are replaced with a set of...
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.