Solved

No Access Control or Constraint Checking Global Variables

Posted on 2011-03-21
5
225 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 100 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 100 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 300 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 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