Property vs Variable of class

  • Thread starter Thread starter Audwin
  • Start date Start date
A

Audwin

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 ?
 
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).
 
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
 
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
 
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" ?
 
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.
 
..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.
 
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.
 
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.

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'sa
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
 
Michael D. Ober said:
.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.
 
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

"Audwin" <[email protected]> a écrit dans le message de news: (e-mail address removed)...
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.
 
Back
Top