Jump to content

  • Log In with Google      Sign In   
  • Create Account

C# Properties or GET/SET methods ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
18 replies to this topic

#1 Avenyr   Members   -  Reputation: 230

Like
0Likes
Like

Posted 11 July 2005 - 01:40 PM

While reading about C# I came upon these Properties that seem to me like they are a simple way of hiding the get/set methods to access and modify variables inside a class and I wanted to know if they had any real advantages over the classical method. so would it be better to do this
private string foreName;
public string ForeName
{
get
{
return foreName;
}
set
{
if (value.Length > 20)
// code here to take error recovery action
// (eg. throw an exception)
else
foreName = value;
}
}

OR This
private string foreName;

public void getForeName()
{
   return foreName;
}

public void setForeName(String name)
{
   foreName = name;//with more code if needed
}

Thanks for your reply !!

Sponsor:

#2 Smack0007   Members   -  Reputation: 157

Like
0Likes
Like

Posted 11 July 2005 - 01:49 PM

Use the first method. Properties are pretty standard throughout C#.

#3 moogleii   Members   -  Reputation: 133

Like
0Likes
Like

Posted 11 July 2005 - 01:56 PM

I think the C# way is mainly for cleanliness. The traditional way, you end up exposing two methods (but it's kinda nice if you like all your gets and sets listed together in the IDE).

#4 Avenyr   Members   -  Reputation: 230

Like
0Likes
Like

Posted 11 July 2005 - 02:36 PM

So basically, would it be correct to assume that these properties were brought in for cleanliness and that either methods will provide the exact same results simply displayed differently ?

#5 intrest86   Members   -  Reputation: 742

Like
0Likes
Like

Posted 11 July 2005 - 02:42 PM

Quote:
Original post by Avenyr
So basically, would it be correct to assume that these properties were brought in for cleanliness and that either methods will provide the exact same results simply displayed differently ?

Not quite. If you ever want to integrate editing of your object into the IDE, you will want everything to use properties instead of seperate get/set functions. The properties grid will only display properties, and can't work with methods.

#6 Bincho   Members   -  Reputation: 194

Like
0Likes
Like

Posted 11 July 2005 - 02:46 PM

According to C# and the .NET Platform it is a better idea to use properties instead of get/set methods as properties integrate better with the .NET base class libraries. It is also mentioned in the book that properties are actually mapped to get and set functions once they are compiled. Another point touched upon is that it is much more syntactically appealing to use properties with the increment/deincrement operators than it is to use said operators with get/set methods.

#7 Avenyr   Members   -  Reputation: 230

Like
0Likes
Like

Posted 11 July 2005 - 04:50 PM

Thanks everyone for replying it has been really helpful so I will go on and use Properties for functionality with VS.NET

But what about C++ or Java ? I must make use of the GET/SET methods because the properties do not exist for those languages I assume.



#8 Smack0007   Members   -  Reputation: 157

Like
0Likes
Like

Posted 11 July 2005 - 05:37 PM

This is true but I like to do things like:


class CFoo
{
private:
int m_x, m_y;
public:
int& X() { return m_x; }
int& Y() { return m_y; }
};



So then I can do things like:


CFoo foo;
foo.X() = 10;
foo.Y() = 20;
cout<<foo.X();
cout<<foo.Y();



You would only want to do this with member variables that can be edited freely.





#9 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 11 July 2005 - 10:18 PM

Quote:
Original post by Smack0007
This is true but I like to do things like:

*** Source Snippet Removed ***

So then I can do things like:

*** Source Snippet Removed ***

You would only want to do this with member variables that can be edited freely.
And what's the benefit over public member variables now?

#10 Toolmaker   Members   -  Reputation: 935

Like
0Likes
Like

Posted 11 July 2005 - 11:15 PM

Quote:
Original post by Smack0007
This is true but I like to do things like:

*** Source Snippet Removed ***

So then I can do things like:

*** Source Snippet Removed ***

You would only want to do this with member variables that can be edited freely.


Using the C# way, it would look like:

public class Foo
{
private Point point;

public point Location
{
get { return point; }
set { point = value; }
}
}

Console.Writeline("Location is: ", fooInstance.Location);




Works perfect, and I love it more than having to add () all the time. Also, if you write your own control, using properties allows the developer to change the behaviour in the forms editor, instead in code.

And anon is right, the code you showed begs to make the X/Y variables public, as returning int& from a function gives you full-access to them, begging to be messed up during runtime.

Also, you use properties because if you need sanity checking later, you edit the property and the usage stays the same.

Toolmaker

Toolmaker salutes mother Tiberia[My website] - [My Dune II Remake Dev Blog]/* -Earth is 98% full. Please delete anybody you can.*/

#11 Walt Destler   Members   -  Reputation: 186

Like
0Likes
Like

Posted 12 July 2005 - 02:55 AM

Do use Get___ and Set___ methods when the implementation of either is particularly slow, memory-intensive, or in any other way behaves unexpectedly. By using properties, you imply to the programmer using them that the implementation is reasonably fast (i.e., it doesn't do more than a simple set/get with maybe a little bit of checking, other minor maintainence).

#12 Smack0007   Members   -  Reputation: 157

Like
0Likes
Like

Posted 12 July 2005 - 06:31 AM

There is no real benefit. It just makes code look readable. I was refering to C++ code, read the post before mine.

#13 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 12 July 2005 - 06:36 AM

Quote:
Original post by Smack0007
This is true but I like to do things like:

*** Source Snippet Removed ***

So then I can do things like:

*** Source Snippet Removed ***

You would only want to do this with member variables that can be edited freely.


Why not simply make x and y public if you're going to let everything access them without restriction like this? What do you gain by doing it this way?

#14 Smack0007   Members   -  Reputation: 157

Like
0Likes
Like

Posted 12 July 2005 - 06:58 AM

My point is this. You see alot of people do this:

class CFoo
{
private:
int m_x, m_y;
public:
int GetX() { return m_x; }
void SetX(int x) { m_x = x; }
int GetY() { return m_y; }
void SetY(int y) { m_y = y; }
};




There is no added benefit to this, but that is the whole idea behind information hiding, blah blah blah. The guy asked if there was anything equivlent to properties in C++ or Java. I said no. Then I offered something similar.

#15 joew   Crossbones+   -  Reputation: 3667

Like
0Likes
Like

Posted 12 July 2005 - 07:10 AM

Quote:
Original post by Smack0007
There is no added benefit to this, but that is the whole idea behind information hiding, blah blah blah.

Actually there is a major benefit to doing it that way. By hiding the implementation you are able to change the layer underneath without breaking the interface for the end-user. This will be paramount in 32/64 bit compatibility and also allows much greater flexibility.

It is the same reason that C# uses properties. Basic getter/setters that hide implementation so that you can change it in the future without affecting the interface and/or breaking current implementations.

#16 Bracket   Members   -  Reputation: 240

Like
0Likes
Like

Posted 12 July 2005 - 07:29 AM

You should definitely use properties rather than writing get/set methods, or using public members. Internally, when you create a property get/set (actually get_ and set_) methods are created for you; this allows you to use reflection to cleanly fill objects that aren't native to your project (I do this in Persist - http://www.csharpninja.com/persist/default.aspx - to load database data into objects with only an XML file describing the object), allows data binding to work with objects rather than DataSets, and really helps reduce coupling in code. There may be a small performance hit, but from what I've read it is largely optimized away - I've never seen my code slow down with it.

It's worth remembering that you can implement JUST a get{} block (or just a set{} block) to give read-only or write-only properties, and you can do whatever you like in the code. I'm fond of collection properties that auto-initialize themselves on the first access, and properties like Age which only implement a get{} and calculate from date of birth rather than a private age variable (so it is always correct when you read it).

The appeal over public member variables is that you abstract your interface by using properties - if you need to add some logic to a property without affecting the interface, it is very easy to do so (it's not really hard to convert a public member into a property, though, especially with a refactoring tool). The downside is that it is more typing!

#17 twkr   Members   -  Reputation: 162

Like
0Likes
Like

Posted 12 July 2005 - 07:47 AM

i think the most useful thing of properties in C# is that you can use reflection to enumerate them. so you can use the properties even if you dont know the names of the properties. in C++ i think you can only do properties with a microsoft compiler. doing something like this:

#define readwrite(sDataType, sPropertyName) __declspec(property(get=Get##sPropertyName,put=Set##sPropertyName)) sDataType sPropertyName

#define readonly(sDataType, sPropertyName) __declspec(property(get=Get##sPropertyName)) sDataType sPropertyName

#define writeonly(sDataType, sPropertyName) __declspec(property(put=Set##sPropertyName)) sDataType sPropertyName

#18 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 12 July 2005 - 07:35 PM

Quote:
Original post by Smack0007
There is no real benefit. It just makes code look readable. I was refering to C++ code, read the post before mine.
IMO this is less readable:


class CFoo
{
private:
int m_x, m_y;
public:
int& X() { return m_x; }
int& Y() { return m_y; }
};

CFoo foo;
foo.X() = 10;
foo.Y() = 20;
cout<<foo.X();
cout<<foo.Y();



than this:


class CFoo
{
public:
int x, y;
};

CFoo foo;
foo.x = 10;
foo.y = 20;
cout<<foo.x;
cout<<foo.y;



Yet they achieve the same information hiding level (i.e. none)

#19 Washu   Senior Moderators   -  Reputation: 5245

Like
0Likes
Like

Posted 12 July 2005 - 08:01 PM


class Foo {
struct IntProperty {
public:
IntProperty(int& val) : val_(val) {}
operator int const&() { return val_; }
IntProperty const& operator=(int i) { val_ = i; return *this; }
private:
int& val_;
};
public:
Foo() : X(x_), Y(y_) {}
IntProperty X;
IntProperty Y;
private:
int x_;
int y_;
};



[grin]

Although, I would never recommend that you actually do something so...ugly.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS