.NET Forum / .NET Framework / New Users / July 2007
Property vs Variable of class
|
|
Thread rating:  |
Audwin - 16 Jul 2007 17:30 GMT In OOP, what is the difference between property and variable, why can't we just use variable in a class implementation. Actually my question is variable and methods are enough. Property seems to be reductant ?
Larry Smith - 16 Jul 2007 18:00 GMT > In OOP, what is the difference between property and variable, why can't we > just use variable in a class implementation. > Actually my question is variable and methods are enough. Property seems to > be reductant ? First of all, you should never expose a member variable directly. Always make it private which is a standard OOP principle. Otherwise you weaken encapsulation by exposing your object's implementation to the outside world. Imagine if you decide to change how your object works later on for instance, say, by changing your variable to some other incompatible type. You would then break all existing clients of your object which would need to be fixed in order to accomodate your change. A "get" and/or "set" function wrapping your variable is the usually remedy for this (allowing you to change things without breaking clients). However, a property is a high-level, built-in construct designed explicitly for this purpose. It's much cleaner than using a "get" and/or "set" function IOW. Not only does it instantly convey its intended purpose to other readers (improving your code's legibility), but it's also specifically recognized by different tools, controls, etc. For instance, you can pass an object to the native "PropertyGrid" control and it will automatically populate the control with all properties in your object. It can only do this however because it's able to identify a "property" unlike an arbitrary "get" and/or "set" function. This is how the Windows forms designer works in Visual Studio for instance. Note BTW that a property need not deal with member variables at all. It can do anything it wants, calculating your property on-the-fly if you wish (though it's normally better to avoid this unless the calculation is extremely simple).
buu - 16 Jul 2007 18:03 GMT if you use some class collections (generics) and put some objects into them, you can change them only through properties, but not through public variables
> In OOP, what is the difference between property and variable, why can't we > just use variable in a class implementation. > Actually my question is variable and methods are enough. Property seems to > be reductant ? Peter Duniho - 16 Jul 2007 18:39 GMT > if you use some class collections (generics) and put some objects into > them, > you can change them only through properties, but not through public > variables Huh?
That's not true at all.
If you can modify a class instance via a public property, it is also possible to allow modification via a public field.
Perhaps you are confusing the original question with the issue of storing reference types (e.g. classes) versus value types (e.g. structs) in collections. But even there, there's no fundamental difference between what is possible using properties and what is possible using fields.
Pete
sloan - 16 Jul 2007 18:03 GMT You should search, its been covered numerous times.
My #1 reason.
Binding works with Properties.
Maintenance. You code in properties now, you future proof your code.
Here is one link: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_t hread/thread/29bee53eb4de041c/a007b287fe1665ab?lnk=st
> In OOP, what is the difference between property and variable, why can't we > just use variable in a class implementation. > Actually my question is variable and methods are enough. Property seems to > be reductant ? schneider - 17 Jul 2007 03:48 GMT At least he asked, which shows he is seeking to improve his skill...
> You should search, its been covered numerous times. > [quoted text clipped - 13 lines] >> to >> be reductant ? Audwin - 17 Jul 2007 14:28 GMT Thanks, I see, but what about in traditional Visual C++, (ie Viusal C++ 6.0 ) and even Java ? The can perform the same function without using "property" ?
> You should search, its been covered numerous times. > [quoted text clipped - 5 lines] > > Here is one link: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/brow se_thread/thread/29bee53eb4de041c/a007b287fe1665ab?lnk=st
> > In OOP, what is the difference between property and variable, why can't we > > just use variable in a class implementation. > > Actually my question is variable and methods are enough. Property seems to > > be reductant ? Rory Becker - 17 Jul 2007 14:47 GMT > Thanks, I see, > but what about in traditional Visual C++, (ie Viusal C++ 6.0 ) and > even Java > ? All these languages CAN do this.
The point is that good programming avoids this technique.
Properties hide/protect the internals of the class.
Public variables are as bad as global variables.
There is no way to determine or limit their access.
-- Ror
Michael D. Ober - 17 Jul 2007 15:30 GMT .NET is the only framework I have run across that treats public instance variables of a class/object differently from public properties. Conceptually, they are the same.
That said, I do agree that by using properties with an internal, private variable, you have more flexibility.
Mike Ober.
>> Thanks, I see, >> but what about in traditional Visual C++, (ie Viusal C++ 6.0 ) and [quoted text clipped - 12 lines] > -- > Rory Jon Skeet [C# MVP] - 17 Jul 2007 21:07 GMT > .NET is the only framework I have run across that treats public instance > variables of a class/object differently from public properties. > Conceptually, they are the same. No, they're not. Properties are just pairs of methods (get and set, where either is optional) - you can have properties which don't have any variables at all.
The *syntax* is similar to field access (for the most part) but they're not conceptually the same.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Audwin - 17 Jul 2007 16:17 GMT I know, Let me clarify. What I am trying to say is that I can still use a public void function to set and get the value of a private variable instead of public property in class implementation.
So, why should I use "property" in c++.net and even Visual Basic 6.0 ? It seems to complicate the program.
> > Thanks, I see, > > but what about in traditional Visual C++, (ie Viusal C++ 6.0 ) and [quoted text clipped - 13 lines] > -- > Rory Peter Duniho - 17 Jul 2007 17:44 GMT > I know, > Let me clarify. What I am trying to say is that I can still use a public [quoted text clipped - 3 lines] > So, why should I use "property" in c++.net and even Visual Basic 6.0 ? > It seems to complicate the program. To use a "public void function" to get a value from a class, you'd have to pass another parameter by reference. That seems kind of messy to me.
Ignoring that though, yes, you're right...in other languages that do not explicitly define the concept of a property, you can still effectively do the same thing by implementing setter and getter functions. For example, instead of this C# code:
private int _i;
public int I { get { return _i; } set { _i = value; } }
You could write this C++ code:
private: int _i;
public: int GetI() { return _i; } void SetI(int i) { _i = i; }
However, those functions don't mean anything special to the compiler in C++. One thing about properties is that they alert the compiler that the member is essentially a public value member of the class, as opposed to some method that just returns some value (whether that's a number that's a result of a calculation, an instance of something being instantiated, etc.) That is, it's not just the result of some computation, but rather some value inherent in the state of the class. Thus the name "property".
There are a variety of benefits to this, most of them relating to code maintainability. But one of the most obvious, non-maintenance-related benefits is that the property can be treated specially by various things. Class serialization works because of properties, as does data binding. You could shoe-horn stuff like that into regular C++ code, but it would be clumsy and clearly hacked in. Properties provide a nice, clean way to construct behavior around the idea of properties of a class.
There's a lot of stuff in .NET, and especially in C# (though I guess managed C++ has its own versions of much of this...haven't used it much myself, so I don't know firsthand), that does really do anything that you couldn't do some other way. What it does do is make it a lot easier, and maintainable.
Where you see something that "complicates the program" I see something that actually simplifies it. Yes, it introduces yet another new concept to the language (potentially complicating the language), but that new concept allows you to in a much simpler way implement the same thing that you should have been doing all along, in a way that makes that implementation more explicit both to you, the compiler, and other components within .NET. That very much simplifies a lot of things.
Pete
Patrice - 18 Jul 2007 11:06 GMT This is actually how it works behind the scene. But for example the object browser and the language will see this as single property instead of having two unrelated functions. Also they could be inconsistant in scope or types. Plus a tool couldn't relate those two functions as being paired unless you are using strict convention, plus you could use unrelated types etc.... Plus you would have to put attributes where ? etc... etc...
IMO this is simplficaiton rather than than a complication.
-- Patrice
I know, Let me clarify. What I am trying to say is that I can still use a public void function to set and get the value of a private variable instead of public property in class implementation.
So, why should I use "property" in c++.net and even Visual Basic 6.0 ? It seems to complicate the program.
"Rory Becker" <RoryBecker@newsgroup.nospam> wrote in message news:b0ac48a016afc8c99698918b0ed0@msnews.microsoft.com... > > Thanks, I see, > > but what about in traditional Visual C++, (ie Viusal C++ 6.0 ) and > > even Java > > ? > > All these languages CAN do this. > > The point is that good programming avoids this technique. > > Properties hide/protect the internals of the class. > > Public variables are as bad as global variables. > > There is no way to determine or limit their access. > > -- > Rory > >
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|