Solved

No Access Control or Constraint Checking Global Variables

Posted on 2011-03-21
5
222 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 41
Clone Visual studio 2013 solution 5 15
Chat Room 1 24
COnsume rest client 6 7
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

12 Experts available now in Live!

Get 1:1 Help Now