?
Solved

No Access Control or Constraint Checking Global Variables

Posted on 2011-03-21
5
Medium Priority
?
231 Views
Last Modified: 2012-05-11
Hi

Can some one please explain with examples.


Regards
Karan Gupta
0
Comment
Question by:KaranGupta
5 Comments
 
LVL 5

Assisted Solution

by:karthika_cts
karthika_cts earned 400 total points
ID: 35188198
No Access Control or Constraint Checking — A global variable can be get or set by any part of the program, and any rules regarding its use can be easily broken or forgotten. (In other words, get/set accessors are generally preferable over direct data access, and this is even more so for global data.) By extension, the lack of access control greatly hinders achieving security in situations where you may wish to run untrusted code (such as working with 3rd party plugins).

Reference: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26900368.html
http://c2.com/cgi/wiki?GlobalVariablesAreBad
0
 
LVL 11

Assisted Solution

by:lenordiste
lenordiste earned 400 total points
ID: 35188271
Global variables can be accessed by any part in your program which means you have no access control . Since any objects can potentially access your global variable, you will have a hard time debugging your software if your global variable was modified unintentionally.

Also, it is better to wrap global variables in get/set constructs to allow for constraint checking. For instance you may have a global variable of type int which in your software should never be below 10. A property (get/set) will allow you to add constraint checking and make sure that nobody tries to assign a number below 10.
0
 

Author Comment

by:KaranGupta
ID: 35188586
I have 2 questions regarding this

1. What is the access specifier of global variable
2. How can be add a constraint check on normal variable.
0
 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 1200 total points
ID: 35189535
Access specifier determines the visibility of the variable/property at any point your app.
Read this article which informs various specifiers in C#
http://en.csharp-online.net/CSharp_Basic_Concepts%E2%80%94Declared_Accessibility

Pls check the code sample to know how we can do the constraint check.
internal class TestGlobal1
    {
        internal void Test()
        {
            TestData.MyData = 15;
            TestData.MyData = -6; // This value will not be allowed to set

            TestGlobal2 test2 = new TestGlobal2();
            test2.Test();

            TestData.MyData = 1; // This value will be allowed to set            
            test2.Test();            

            Console.ReadKey();
        }
    }

    internal class TestGlobal2
    {
        internal void Test()
        {
            Console.WriteLine("Data = " + TestData.MyData);
        }
    }

    internal class TestData
    {
        private static int _myData; // Class level static variable will be a global variable for all objects

        // This is the property which exposes the value across the app.
        internal static int MyData
        {
            get { return _myData; }
            set
            {
                if (value > 0)
                {
                    // Allow only +ve integers
                    _myData = value;
                }
                else
                {
                    // Optionally u can throw exception here
                }
            }
        }
    }

Open in new window

0
 

Author Closing Comment

by:KaranGupta
ID: 35195945
Thanks
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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

809 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