Differencebetween a Property and a Variable

What is the difference between a property and a variable

As many have pointed out, A is a field, B is a property.

The real question is, why should you care, and what to use?

I refer to a blog post of Jonathan Aneja:

(Its in VB, but it applies to C# as well ;))

So why use properties over fields, 5 reasons:

1. Fields can’t be used in Interfaces

You can’t enforce the existence of a
field in an object’s public contract
through an interface. For properties
though it works fine.

2. Validation

While your application currently may
not require any validation logic to
set a particular value, changing
business requirements may require
inserting this logic later. At that
point changing a field to a property
is a breaking change for consumers of
your API. (For example if someone was
inspecting your class via reflection).

3. Binary Serialization

Changing a field to a property is a
breaking change if you’re using binary
serialization. Incidentally, this is
one of the reasons VB10’s
auto-implemented properties have a
“bindable” backing field (i.e. you can
express the name of the backing field
in code) – that way, if you change an
auto-implemented property to an
expanded property, you can still
maintain serialization compatibility
by keeping the backing field name the
same (in C# you’re forced to change it
because it generates backing fields
with unbindable names).

4. A lot of the .NET databinding infrastructure binds to properties but not fields

I’ve heard arguments on both sides as
to whether or not that’s a good thing,
but the reality is that’s the way it
works right now. (Note from me: WPF bindings work on properties)

5. Exposing a public field is an FxCop violation

For many of the reasons listed above
:)

There might be more reasons.

I would also like to point to a blog post of Jeff Atwood and conclude with a quote from it:

The really important thing to take away here is to avoid writing code that doesn't matter. And property wrappers around public variables are the very essence of meaningless code.

What is the difference between a property and an instance variable?

Iain, this is basically a terminology question and is, despite the "language-agnostic" tag associated with this question, very language/environment related.

For design discussions sake, property and instance variable can be used interchangeably, since the idea is that a property is a data item describing an object.

When talking about a specific language these two can be different. For example, in C# a property is actually a function that returns an object, while an instance variable is a non-static member variable of a class.

diff between variable and property?

Simply you can't validate the data being stored in variable but in a Property you can.
A Propery is more like a method as it has get & set methods which you define to handle the data.

Also they are usefull in Databinding.

Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.



Example:

like in case of Date of Birth. You need to validate that age is less then todays date:

In a Field this is valid:

DateTime age = new DateTime(2012,12,1);//1 december 2012

But in a property you can validate it:

private DateTime _dob;
public DateTime dob
{
get
{
if(_dob!= null) return _dob;
}
set
{
if(DateTime.Compare(value, DateTime.Today) <= 0)
{
_dob= value;
}
else
{
throw new System.InvalidOperationException("Date of Birth should be less then today's date");
}
}
}

What is the difference between a property and a variable in Swift?

Local variables are just things that you work with. You have full control over these, and if you change a variable in a function, nothing outside of your function is ever gonna know. If I write a framework and you use it, and I decide to change something about a function's local variables, your app that uses my framework will keep working just as if nothing changed.

Classes, on the other hand, describe a contract. When you use a class, you have access to everything they publicly advertise. This means that if I write a framework and you use it, if I ever change or remove a public member on a class, your code will break if you were previously using that member.

For this reason, in many languages, it's bad practice to mark instance variables as public. Instance variables having no logic attached, if I want at some point to trigger something when a field is changed or if I want to remove the field entirely (and instead report a value in a sub-object or something), then I'm stuck with changing the public contract (turning the field in a pair of get/set methods, for instance), and possibly breaking your code.

Swift makes properties an indirection for this reason. Swift properties can be treated as dumb values for the most part, but if you ever need to change from a stored value to a computed value or something, you can do it without changing your class's interface. That way, you don't break existing code that relies on the property.

What is the difference between field, variable, attribute, and property in Java POJOs?

From here: http://docs.oracle.com/javase/tutorial/information/glossary.html


  • field

    • A data member of a class. Unless specified otherwise, a field is not static.

  • property

    • Characteristics of an object that users can set, such as the color of a window.

  • attribute

    • Not listed in the above glossary

  • variable

    • An item of data named by an identifier. Each variable has a type, such as int or Object, and a scope. See also class variable, instance variable, local variable.

C# , Difference between property with variable and without variable

The later is called an Automatic Property and is the same.They were introduced in C#3, you can read more about them here: http://trashvin.blogspot.com/2008/05/automatic-properties-and-object.html

Simply put, Automatic Properties are syntactic sugar so the developer has to type less code and the compiler will generate the private field and the public setter and getter for you.

What is the difference between javascript property and javascript variable?

= is for object property or global/local variable assignment.
: is only for property assignment at object definition.

Also:
You can delete a property.
You cannot delete a variable.

var obj = {
p1: 'im p1',
p2: 2
};
obj.p1 = 'im updated p1'; // assign a new value to object property
var v = 'just a var'; // is global outside a function and local inside a function

delete obj.p1; // is ok
delete v; // is not ok

What is the difference between Method, Property and Variable in PHP

Yes, method is a function.

Model property would be a global variable within the class, so you can use it in all the methods. And depending on the access modifier (private, protected, public) the model property can be used from other classes inheriting/instantiating from that class.

While a variable will be something used within a method and has usage only within the body of that method.



Related Topics



Leave a reply



Submit