?
Solved

What is difference between proerty and global variable?

Posted on 2011-02-16
10
Medium Priority
?
426 Views
Last Modified: 2012-05-11
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
Comment
Question by:TheCommunicator
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 8

Accepted Solution

by:
mac-will earned 672 total points
ID: 34911378
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
 
LVL 8

Expert Comment

by:mac-will
ID: 34911392
Also C# does not have "global variables" like C/C++

I assumed you meant public class fields.  Was that correct?
0
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34911396
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:TheCommunicator
ID: 34911589
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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 332 total points
ID: 34911846
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
 
LVL 9

Assisted Solution

by:gdupadhyay
gdupadhyay earned 664 total points
ID: 34911876
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
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 332 total points
ID: 34913015
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
 
LVL 9

Assisted Solution

by:gdupadhyay
gdupadhyay earned 664 total points
ID: 34917832
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
 
LVL 8

Assisted Solution

by:mac-will
mac-will earned 672 total points
ID: 34919870
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
 

Author Comment

by:TheCommunicator
ID: 34922251
Thank you so much guys. each author's comments was equally helpful and I am really thankful to you guys, :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question