Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
Difference between a property and variable
Posted on: 29 Sep 2009

Can anyone explain me difference between a property and variable. I found them more or less same like both are used for storing and retrieving values.
 
I saw a code where player info has to be strored and for that some properties were declared inside a class. Property names are Name, PlayerNo, Age.
code:
private string name;
public string Name
{
get
{return name;}
set
{name =value;}
}
..................
..................
 
But we can easily do this task using variables.
So is there any advantage of using properties instead of variables.

AuthorReply
Kirtan Patel
  • 8
  • 0
accepted
Re: Difference between a property and variable
Posted on: 02 Oct 2009   Accepted Answer
Friend, Here is Explanation .

dont forget to mark " do you like answer" if answer help you :)

Properties are Useful when we are accessing Private Section of the Class


Like

class TestClass

{

 

private string x = "Test Test";


void TestFun()

{

   Console.WriteLine(x);

}

 

 

public string ChangeName

{

    getreturn x;}

    set{   x  = value;}

}

 

}



Now We are Calling the TestFun  Function

by Following code

void main()

{

       TestClass obj = new TestClass();

       obj.TestFun();

 

}


above code will print "Test Test" .

Now What if you want to change the value of X in TestClass ??????

you can only do it by Setting Property of Object of Class

See Below Code

void main()

{

       TestClass obj = new TestClass();

       obj.ChangeName = "Property Testing ";

       obj.TestFun();

}



Now It will Change the Value of X in Object of class and will Print " Property Testing "

Conclusion We have Changed the Private Part of the Class that is Never Possible using Variable because if Member Variable of Class is declared as Private we can not access it

By Property we are allowing User to Access only portion we want user to change .

MY BLOG ( www.codestructs.com ) | MY SITE

Kirtan
 Patel   BCA   MCA (SEMESTER-IV)  MINDCRACKER MVP WEB HACKER



Re: Difference between a property and variable
Posted on: 30 Sep 2009  

The activity of both are almost same. But only differce is, We can change or do any calculations before assign the value into the property but not in variable
See the following example it will give you clear idea.
private string name;
public string Name
{
get
{return "Mr."+ name;}
set
{name ="Mr." + value;}
}
In the above exaple we are assign the "Mr" in front of the name which is assigned. Likewise wise we can do many calculations before assign the value of the property. But we cannt do it in the simple variables..
I think You catch my point...

 
 

By

Raji

Re: Difference between a property and variable
Posted on: 30 Sep 2009  
Hi
This is the basic definition of variable and property.

A variable corresponds directly to a memory location. You define a variable with a single declaration statement. A variable can be a local variable, defined inside a procedure and available only within that procedure, or it can be a member variable, defined in a module, class, or structure but not inside any procedure. A member variable is also called a field.

A property is a data element defined on a module, class, or structure. You define a property with a code block between the Property and End Property statements. The code block contains a Get procedure, a Set procedure, or both. These procedures are called property procedures or property accessors. In addition to retrieving or storing the property's value, they can also perform custom actions, such as updating an access counter.


If u want more difference then take the reference of this article.

http://msdn.microsoft.com/en-us/library/sk5e8eth(VS.80).aspx




Re: Difference between a property and variable
Posted on: 30 Sep 2009  
properties allow you to connect to the properties via data binding  but   fields do not ...

VCDEBUGGER
Re: Difference between a property and variable
Posted on: 02 Oct 2009  

sorry but we can do these activities with simple variable also.
string var = "name";
string newvar = "Mr."+var;
Console.WriteLine(newvar);

Re: Difference between a property and variable
Posted on: 02 Oct 2009  

Hello Gaurish,
The differences you are giving and given on the link are for VB.
Do they all also apply to c sharp properties.
Still i am not clear about properties.

Re: Difference between a property and variable
Posted on: 03 Oct 2009  
Hi

i would tell to read this to learn complete sample code and details

http://www.codinghorror.com/blog/archives/000654.html

thank you

If its helped to you, make it green
Re: Difference between a property and variable
Posted on: 18 Mar 2011  
The difference between variable and property was explained by Kirtan Patel.

Declaring instance(object) variables with private(access modifier) is known as information hiding(encapsulation).
   When an application creates(instantiates) an object of Class, variables name,playerNo,age are encapsulated in the instance(object)
   and can be accessed ONLY by methods and properies of the object's class.

Re: Difference between a property and variable
Posted on: 18 Mar 2011  
The crucial difference between them is that a variable represents a memory location but a property does not. A public read/write property, Prop of type T, corresponds to two methods:


public T get_Prop() { \\ code }
public void set_Prop(T value) { \\ code }


Usually a property provides access to a private field, though it need not do so.



Employers - Post Free Jobs

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.