Solved

What is difference between proerty and global variable?

Posted on 2011-02-16
10
421 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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 8

Accepted Solution

by:
mac-will earned 168 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
 

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 83 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Assisted Solution

by:gdupadhyay
gdupadhyay earned 166 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 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 83 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 166 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 168 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now