• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

What is difference between proerty and global variable?

Sometimes it makes me wonder that why do we have to use properties all the times when all they are used for is to hold some values while execution is going on?

Can't we use global variables for that? I mean I see that when we serialize and de-serialize the objects, we can take advantage of proprties being there but other than that what exact benefits that properties give as as opposed to the public variables?
0
TheCommunicator
Asked:
TheCommunicator
  • 3
  • 3
  • 2
  • +2
6 Solutions
 
mac-willCommented:
Properties in C# play the role of get and set methods.

These method can be used for several things such as data validation.

Say you have a class that holds a number but that number must be between 5 and 10:


private int _num;
public int Num
{
get{return _num;}
set{
 if(value >5 && value <10)
{
_num = value;
}
else
{
//handle this somehow...
}
}
}

Open in new window

0
 
mac-willCommented:
Also C# does not have "global variables" like C/C++

I assumed you meant public class fields.  Was that correct?
0
 
gdupadhyayCommented:
global is static and should be not a part of class object. proerty  can be a part of class object.

See following URL:

http://dotnetperls.com/global-variables-aspnet


Let me know for more information
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
TheCommunicatorAuthor Commented:
Thank you so much guys. I am new to C# but the question here is why did C# people decide to introudce the whole new concept of Static properties rather than traditional C++ style properties?

I understand the point that mac-will raised that it does the validation but other than that GET and Set methods are nothing but  methods to retrieve value of that property whcih can be easily done with Public variables, right?

0
 
Carl TawnSystems and Integration DeveloperCommented:
You can do the same thing with public variables if you only want to read/write a straight value. The point of properties is "encapsulation", that means hiding the details of the implementation from anything that uses your object.

You may have something like:
public class Cat
{
     public int Age;
}

Open in new window

Which is fine if age is stored locally and never changes. However, if you ever wanted to change where or how "Age" was stored/retrieved (or even change Ages data type) then you would have to recode anything that uses your Cat object to be able to deal with the change. However, if you expose it as a property then you are free to change it's implementation without having to alter the clients.
0
 
gdupadhyayCommented:
I think you need to know first about static.

http://msdn.microsoft.com/en-us/library/98f28cdx(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

Singleton can be achieved by using static properties.
0
 
anarki_jimbelCommented:
Minor correction to gdupadhyay's post: static isn't global. Static is just a class level variable (as oposed to instance level variable). It can be private, by the way.

Have a look:

http://www.eggheadcafe.com/articles/20020206.asp

carl_tawn is absolutely right - you can use both public vars and properties. And he told about encapsulation. It's just a better and cleaner approach to use properties. And better from code maintenance perspective...
0
 
gdupadhyayCommented:
I said: "global is static" not static is global.

In .net we don't have any Module to declare global variables. so we can declare a public class with public static variable which can be across all the namespace.
0
 
mac-willCommented:
I would not confuse someone learning C# with the semantics of global and static.  I think is more accurate to say C# does not have global variables in the same sense C/C++ does.

Answering the original authors question about why Properties exist in C# at all is a little more tricky.  Basically the authors of the language decided to add these special methods because they thought it was an advantage in terms of code readibility and maintainibility...

When it boils down to in these properties are exactly the same a get and set methods (just look at the IL)

That said I still think there is more to properties than just validation.

Properties can encapsulate any kind of data in a clean consistant way NOT just class variables.

For example I often interface with dedicated hardware and I might encapsulate some communication checks in a public property as follows:

class MyHardwareModule
{

public bool ModuleIsPoweredOn
{
    get
    {
        if(module.write_test_to_see_if_unit_is_on)
        {
              return true;
        }
        else
        {
              return false;
        }
}
}

Open in new window


There are lots of neat ways to use Properties and they do not always simply have to wrap a member variable.
0
 
TheCommunicatorAuthor Commented:
Thank you so much guys. each author's comments was equally helpful and I am really thankful to you guys, :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now