W
win
what is the different of the member variable and property?
Thanks a lot.
Vincent
Thanks a lot.
Vincent
what is the different of the member variable and property?
win said:what is the different of the member variable and property?
Thanks a lot.
Vincent
More and more I get confused why I need a property.
Michel said:Encapsulation is one verry important reasson
public int Tom {get;set;} is an automaticaz property i.e; behind the scene
you have a _Tom variable and the code that get sets the variable is crated
for you by the compiler. Yo'ull have this also in VB10.
Now the idea is that if you expose directly a field (member variable) you
have no way to do something tis value si defined. IOn the other hand when
suign a property it llkks the same but your code runs so you have a chance
to take some action.
I you decide to swicth from the former to the later the existing code breaks
as technically this is not the same (i.e this is a rezal affection in the
first case, a call in the second case). So a good practice is to always use
properties so that if you need at a lter time to add some custom code when
then value is changed, you'll be able to do so withoput breaking the
exisitng compiled code...
Michel Posseth said:Encapsulation is one verry important reasson
regards
Michel
Tom,
That does not change anything.
A public field is an accessor and a public property is an assesor.
It makes sense as it is only a get or a set which is public or as you wrote
only one of those.
But I cannot see what encapsulation is done with a public property which
only gets the value from a private field and set the given value in a
private field.
But I must miss something so some persons are writting about this that it is
very important.
(I know that a field does not show up in a property grid however I mean a
real difference when this kinds of things are not needed)
Cor
Tom said:But it does make a difference Cor... If you are implying that you
don't need properties, then you are correct properties are not
needed. They are as I said, just a highlevel abstraction of a
getValue/SetValue method call pair - but, with more convenient syntax
The major point being made is that exposing a member variable
directly to outside of the containing class is a bad idea. For
example, what happens to all existing client code if suddenly you
need to add validation to a member field? Or you want to implement
change notification? Or you realize that the member field really
should be a calculated value? I'll tell you what happens - all the
code accessing the value - even if you don't change the name breaks.
If you had just used a property in the first place, then client code
would
be blisfully ignorant of the change....
Encapsulation is a good thing, because it insulates client code from
changes in implementation.
Yes, but removing a field and adding a property is not a change in
implementation.
additional overhead now.
I write code for now and not for later. If it is
not even planned to encapsulate anything in the future I don't need a
property for this.
If you plan to implement anything you wrote above, then a property is
fine. Otherwise there are too many "Ifs": If this or that will happen...
well, maybe - or maybe not? In general, I write the minimum code that I
need now and I take into account things that are planned for the future.
I don't know how to write code considering imaginary things that
might happen in the future because 1 million things can happen in the
future. I am not able to include them all now. This starts from needing
a property and ends in becoming a brick layer.
Or, back to practice, what if you will rethink the object design in future?
The class might become obsolete, so why do you write the class now at all?
A few other reassons
In .NET you can use the members of a specified class to bind a control, such
a combo box or a list.
this can only be done with properties
As i am a business developer and was once a commercial library developer i
follow ( try to ) conform to MSF rules
which means checking your code as if it were to be a reusable library (
OOP ) , In this context, the difference in binary calling convention
between fields and properties would matter, because you would not have the
option of simply recompiling all your callers' code.
So from my perspective public class variabels are a big red dot in code
analysis ,a absolute no go area
i just can`t understand why a coder would criple his app on forehand , as i
can do with properties what he can`t but he can`t do what i can with
properties
and after a release to the outside world he has really a problem if he sees
the light .
above is just my humble opinion
regards
Michel Posseth
Michel said:Do you mind if i am a bit dissapointed about that question ? ;-(
http://en.wikipedia.org/wiki/Encapsulation_(computer_science)
Properties are an important aspect of that concept
as a property can react towards field retrievals or settings
this is necesary for value validation , value calculations ,ability to
override , event triggering
Properties are one of the fundaments of OOP for the above reassons
Michel said:A few other reassons
In .NET you can use the members of a specified class to bind a
control, such a combo box or a list.
this can only be done with properties
So from my perspective public class variabels are a big red dot in
code analysis ,a absolute no go area
i just can`t understand why a coder would criple his app on forehand
, as i can do with properties what he can`t but he can`t do what i
can with properties
Armin Zingler said:Right, but you are drawing the wrong conclusion (IMO). My conclusion
is that
it's a missing feature that you can not bind to public fields.
Tom Shelton said:If you create a class, and publish and interface that exposes a field,
then
change that field access to a property, then you have broken all existing
clients if they update the library.