• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Avenyr

C# Properties or GET/SET methods ?

18 posts in this topic

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 !!
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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 ?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites
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.



0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


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

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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!
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0