Welcome to Dream.In.Code
Become a C# Expert!

Join 150,395 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,043 people online right now. Registration is fast and FREE... Join Now!




Properties

 
Reply to this topicStart new topic

Properties, What's the point

Echilon
3 Mar, 2008 - 02:31 AM
Post #1

New D.I.C Head
*

Joined: 23 Feb, 2007
Posts: 12


My Contributions
I'm working my way through Murach's C# 2005, and I'm still quite new to C#, but I'm having difficulty understanding the advantage of using properties. Say I have a variable, myString - what would be the advantage of using a property for it over just declaring it public?

For example:
CODE

public string myString;


Vs.

CODE

private string myString;
public MyString {
  get { return myString; }
  set { myString = value; }
}

User is offlineProfile CardPM
+Quote Post

bhandari
RE: Properties
3 Mar, 2008 - 02:44 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
I will have to write something like:

CODE

myString str = new myString();
str.myString = "somevalue";


in second version,
CODE

myString str = new myString();
str.set("somevalue")


second one is more easy to read.
if you don't want me to give some value to your myString variable, you can delete that setter!!!
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Properties
3 Mar, 2008 - 06:13 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,290



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Good question! It this example, there is no real advantage, except encapsulation. The advantage comes in future changes.

Pretend at some point you want the property read only, just remove the set. Even more complex, say you want that propery only to be updateable if a certain criteria is met, you can have something like:
CODE

public string MyString {
    get { return myString; }
    set {
        if (value == null) { throw new System.ArgumentNullException("MyString can't be null!"); }
        if (value.Length<5) { throw new System.ArgumentException("MyString cannot be less than 5 characters."); }
        myString = value;
    }
}


Even if you never do this, you have the ability to and that's really the point. For instance, you may want to track all changes to an object at some point. You can put a logger in set or even attach it to an event.

This is part of a fundamental OO design principal of only exposing as much of an object as you need to. That way, you can add or change object code later without impacting the rest of the code that uses the object. If you see a public variable in an object, it's scope is ususally very small, otherwise extra code, like wrapping it in a property, should be written.

Hope this helps.

User is online!Profile CardPM
+Quote Post

Echilon
RE: Properties
3 Mar, 2008 - 10:31 AM
Post #4

New D.I.C Head
*

Joined: 23 Feb, 2007
Posts: 12


My Contributions
Thanks for the help. Properties actually make a lot more sense than standard variables, especially if you need to do validation.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month