What's the purpose to use Property with set and get to change a field?
-
As I know from OOP theory, the purpose of private Properties is to have the variables of an object protected from other classes, that could accidentaly change their value, thus provoking complications in a software. Am I correct till here? Then I started to read C# and I found out that variables are called fields and they are also usually private. But there are also Properties with set and get which are used to change variables' (fields') values. For instance: I have a variable category in a class Myclass. To follow strict C# grammar I write
private int cat;
and then I create a property with set and getpublic int Category { get { return cat; } set { cat = value; } }
Therefore I can access the property from any other class usingMyobject.Category = 5
And here comes the question: why use such a complicated way? Why not use directly a public cat field? After all, cat is still not protected since I can directly change Category instead. Thanks in advance for any reply.Hi, see these links for some explanation: http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx[^] http://msdn.microsoft.com/en-us/library/w86s7x04%28VS.80%29.aspx[^] Hope it helped!
-
Hi, see these links for some explanation: http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx[^] http://msdn.microsoft.com/en-us/library/w86s7x04%28VS.80%29.aspx[^] Hope it helped!
Thanks for the links. Still, it confuses me a bit, in the way that I have to give two different names in order to describe one object variable. like in the example above. The word 'Category' has a meaning for my class, but I am obliged to use a different word for the field, therefore I chose cat, but I find it poor in a semantic way. Also, I am wondering if that is a Microsoft's "invention" or a general OOP thinking. Does it exist in Java too? I never met it in C++, MFC or other frameworks, nor in UML.
-
As I know from OOP theory, the purpose of private Properties is to have the variables of an object protected from other classes, that could accidentaly change their value, thus provoking complications in a software. Am I correct till here? Then I started to read C# and I found out that variables are called fields and they are also usually private. But there are also Properties with set and get which are used to change variables' (fields') values. For instance: I have a variable category in a class Myclass. To follow strict C# grammar I write
private int cat;
and then I create a property with set and getpublic int Category { get { return cat; } set { cat = value; } }
Therefore I can access the property from any other class usingMyobject.Category = 5
And here comes the question: why use such a complicated way? Why not use directly a public cat field? After all, cat is still not protected since I can directly change Category instead. Thanks in advance for any reply.Let me give you the strong need of property in C#. Suppose there's one field
CustomerID
and you have assignedCustomerIDProperty
to get and set values forCustomerID
. Now After a years requirements being changed and They say they need some validation onCustomerID
now you can not change or not advisable to put a check ofCustomerID
everywhere in the code, At that time you can simply put check at set method of CustomerIDProperty to be rescued. Property is introduced because of the changing requirement in software field. "Walking on water and devloping a software both are the easy things,Provided both FROZEN".Regards, Hiren.
My Recent Article: - Way to know which control have raised PostBack
My Recent Tip/Trick: - Remove HTML Tag, get plain Text -
As I know from OOP theory, the purpose of private Properties is to have the variables of an object protected from other classes, that could accidentaly change their value, thus provoking complications in a software. Am I correct till here? Then I started to read C# and I found out that variables are called fields and they are also usually private. But there are also Properties with set and get which are used to change variables' (fields') values. For instance: I have a variable category in a class Myclass. To follow strict C# grammar I write
private int cat;
and then I create a property with set and getpublic int Category { get { return cat; } set { cat = value; } }
Therefore I can access the property from any other class usingMyobject.Category = 5
And here comes the question: why use such a complicated way? Why not use directly a public cat field? After all, cat is still not protected since I can directly change Category instead. Thanks in advance for any reply.Because you can then provide validation code. But if you don't want to, then try an automatically-implemented property instead.
-
As I know from OOP theory, the purpose of private Properties is to have the variables of an object protected from other classes, that could accidentaly change their value, thus provoking complications in a software. Am I correct till here? Then I started to read C# and I found out that variables are called fields and they are also usually private. But there are also Properties with set and get which are used to change variables' (fields') values. For instance: I have a variable category in a class Myclass. To follow strict C# grammar I write
private int cat;
and then I create a property with set and getpublic int Category { get { return cat; } set { cat = value; } }
Therefore I can access the property from any other class usingMyobject.Category = 5
And here comes the question: why use such a complicated way? Why not use directly a public cat field? After all, cat is still not protected since I can directly change Category instead. Thanks in advance for any reply.nstk wrote:
As I know from OOP theory, the purpose of private Properties is to have the variables of an object protected from other classes, that could accidentaly change their value, thus provoking complications in a software. Am I correct till here?
Only partially. The other reason is to hide the implementation, so that it can change in the future. The calling code only needs to know the property name (or private function name). However you can change how you calculate the value. So it's for future change, as well as current privacy. That's where the complication of properties comes in handy. Next month, you might rewrite your Category property to look like this public int Category { get { if (cat < 0) return 0; else return cat; } set { cat = value; } }
-
Thanks for the links. Still, it confuses me a bit, in the way that I have to give two different names in order to describe one object variable. like in the example above. The word 'Category' has a meaning for my class, but I am obliged to use a different word for the field, therefore I chose cat, but I find it poor in a semantic way. Also, I am wondering if that is a Microsoft's "invention" or a general OOP thinking. Does it exist in Java too? I never met it in C++, MFC or other frameworks, nor in UML.
nstk wrote:
The word 'Category' has a meaning for my class, but I am obliged to use a different word for the field, therefore I chose cat, but I find it poor in a semantic way.
Typically you will use Category for the property name and category or _category for the local variable name. The same OO concept existed in C++ - it was simply a public method though. Properties are not necessary, it's just a new way of saying "public method that returns a value and keeps the value private." In C++ you'd simply write a public function, and some people even used the word "Get". public int GetCategory() { return category; } public void SetCategory(int i) { category = i; } Same thing. However C# property offers you a convenient way of not even declaring the local variable! For example this works without even declaring your local variable called "cat". public int Category { get; set; } Again, this is for future use, where you have the option of changing how Category gets implemented (you can add a variable later, or write the get or set methods with some different code, but the caller would never need to know.) In the mean time, the compiler generates a local variable under the covers for you.
-
Let me give you the strong need of property in C#. Suppose there's one field
CustomerID
and you have assignedCustomerIDProperty
to get and set values forCustomerID
. Now After a years requirements being changed and They say they need some validation onCustomerID
now you can not change or not advisable to put a check ofCustomerID
everywhere in the code, At that time you can simply put check at set method of CustomerIDProperty to be rescued. Property is introduced because of the changing requirement in software field. "Walking on water and devloping a software both are the easy things,Provided both FROZEN".Regards, Hiren.
My Recent Article: - Way to know which control have raised PostBack
My Recent Tip/Trick: - Remove HTML Tag, get plain TextHiren Solanki wrote:
Let me give you the strong need of property in C#. ... Property is introduced because of the changing requirement in software field.
Property was introduced just as a convenience, not because of any need. The change requirement could easily be implemented with normal methods as in C++. It's a quicker way to code for the developer, it's a quicker way to call for the consumer (no parentheses required), offers an auto-generation option, and signifies intent of the developer with fewer comments (a property is used when there is little calculation and no side effects). None of these are necessary though.
-
Hiren Solanki wrote:
Let me give you the strong need of property in C#. ... Property is introduced because of the changing requirement in software field.
Property was introduced just as a convenience, not because of any need. The change requirement could easily be implemented with normal methods as in C++. It's a quicker way to code for the developer, it's a quicker way to call for the consumer (no parentheses required), offers an auto-generation option, and signifies intent of the developer with fewer comments (a property is used when there is little calculation and no side effects). None of these are necessary though.
Also that Interfaces can specify properties, but not fields.
-
Hiren Solanki wrote:
Let me give you the strong need of property in C#. ... Property is introduced because of the changing requirement in software field.
Property was introduced just as a convenience, not because of any need. The change requirement could easily be implemented with normal methods as in C++. It's a quicker way to code for the developer, it's a quicker way to call for the consumer (no parentheses required), offers an auto-generation option, and signifies intent of the developer with fewer comments (a property is used when there is little calculation and no side effects). None of these are necessary though.
It also gives hints at what to look for when reflecting through an object. IE Some asp.net data grids will auto bind columns to all properties of an object.
-
It also gives hints at what to look for when reflecting through an object. IE Some asp.net data grids will auto bind columns to all properties of an object.
kevinnicol wrote:
Some asp.net data grids will auto bind columns to all properties of an object.
That's true and good point, but that's a .NET issue that wasn't applicable to C++.
modified on Friday, December 17, 2010 2:40 PM
-
kevinnicol wrote:
Some asp.net data grids will auto bind columns to all properties of an object.
That's true and good point, but that's a .NET issue that wasn't applicable to C++.
modified on Friday, December 17, 2010 2:40 PM
oops, thought I was in the C# forums, must have stumbled into the C++ forums by accident.
-
oops, thought I was in the C# forums, must have stumbled into the C++ forums by accident.
kevinnicol wrote:
oops, thought I was in the C# forums, must have stumbled into the C++ forums by accident
The point is to answer in context of the OP's questions, and if you had taken the time you spent on your clever sarcasm and spent it on reading all the OP's posts instead, you'd know he was coming from an OO background from C++. So I think you need to decide if you want to help someone with a question in context, or if you want to show off your mad skilz :) The OP said "I am wondering if that is a Microsoft's "invention" or a general OOP thinking. I never met it in C++." By way of my response to you, I was telling him that data grid data binding with properties is a Microsoft-specific invention for C#/.NET, and not general OOP thinking. I replied to your post to help him and the lurkers, not educate you. But as a token of goodwill, I'm still willing to educate you. You misused "IE" in your previous post. :) You might want to look that one up.
-
kevinnicol wrote:
oops, thought I was in the C# forums, must have stumbled into the C++ forums by accident
The point is to answer in context of the OP's questions, and if you had taken the time you spent on your clever sarcasm and spent it on reading all the OP's posts instead, you'd know he was coming from an OO background from C++. So I think you need to decide if you want to help someone with a question in context, or if you want to show off your mad skilz :) The OP said "I am wondering if that is a Microsoft's "invention" or a general OOP thinking. I never met it in C++." By way of my response to you, I was telling him that data grid data binding with properties is a Microsoft-specific invention for C#/.NET, and not general OOP thinking. I replied to your post to help him and the lurkers, not educate you. But as a token of goodwill, I'm still willing to educate you. You misused "IE" in your previous post. :) You might want to look that one up.
There was no sarcasm intended, I just saw your reply in my email and truly thought I had made a reply in the wrong forum. There was no intent of sarcasm or showing of my "mad skilz". I will choose to take your token of goodwill as that and not passive aggression; in the future will use eg. instead of ie. in those cases. Thanks.
-
There was no sarcasm intended, I just saw your reply in my email and truly thought I had made a reply in the wrong forum. There was no intent of sarcasm or showing of my "mad skilz". I will choose to take your token of goodwill as that and not passive aggression; in the future will use eg. instead of ie. in those cases. Thanks.
kevinnicol wrote:
There was no sarcasm intended
Oh sorry, my bad! :doh: Since you already know about e.g., you're ahead of 90% of the people out there....
-
kevinnicol wrote:
There was no sarcasm intended
Oh sorry, my bad! :doh: Since you already know about e.g., you're ahead of 90% of the people out there....
Everybody knows about exempli gratia and id est. They are learned in elementary school.
-
Everybody knows about exempli gratia and id est. They are learned in elementary school.
ha ha yeah, right! If I only had a nickel for every time i.e. got misused.....