Solved

Global variables in C#

Posted on 2006-06-20
8
429 Views
Last Modified: 2008-01-09
Hi Experts,

    I am working with a Windows Form application.  I have a few forms and classes.  I need a way to share common variables between them.  Most of these are constants that I will need to use all over the place.  I can think of a million ways to do it, but I'm not sure what the proper way of doing it is.  What's in accordance with good programming practice?

Thanks,
axnst2
0
Comment
Question by:axnst2
  • 4
  • 3
8 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16942361
C# doesn't allow global variables. Use static variables for this:

class A
{
    public static int x;
}

can be accessed from any place of the program:

A::x = 0;

Public constant variables can be accessed by the same way:

class A
{
    public const int n = 10;
}

int m = A::n;
0
 

Author Comment

by:axnst2
ID: 16942709
I get the following error when I try that:

The namespace alias qualifier '::' always resolves to a type or namespace so is illegal here. Consider using '.' instead.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 16943042
Oops, sorry, this is C++ syntax. Should be:

A.x = 0;

int m = A.n;

0
 

Author Comment

by:axnst2
ID: 16943181
That doesn't expose variables.  Only functions, methods, and properties.  A variable that's global to Class A can't be accessed that way.  Does this mean that I'll have to have a get/set property for all of my variables?  That would be a step backward from VS 6.0.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Expert Comment

by:AlexFM
ID: 16943228
namespace test
{
    class A
    {
        public static int x;
    }

    class Program
    {
        static void Main(string[] args)
        {
            A.x = 1;

            Console.WriteLine(A.x.ToString());
        }
    }
}

Result is:
1
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16943251
That should be exactly what it does..

    public partial class Form1 : Form
    {
        public Form1()
        {
            B temp = new B();

            // here, B._firstVar is equal to 1 ( the value of A.first )
            InitializeComponent();
        }
    }


    class A
    {
        public static int first = 1;
    }

    public class B
    {
        private int _firstVar = 0;
       
        public B()
        {
            _firstVar = A.first;
        }
    }
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16943252
In any case, using properties instead of direct variable access is always better. Constants can be accessed directly.

namespace test
{
    class A
    {
        static int x;

        public const int n = 10;

        public static int X
        {
            get { return A.x; }
            set { A.x = value; }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            A.X = 1;

            Console.WriteLine(A.X.ToString());
            Console.WriteLine(A.n.ToString());
        }
    }
}
0
 

Author Comment

by:axnst2
ID: 16943361
OK....I screwed up.  I actually instantiated the class that has my global variables instead of just referencing the class as a type.

I did this:

CGlobal myGlobalClass = new CGlobal ;
myGlobalClass.x=2;

, instead of this:

CGlobal.x=2;

My BAD!!!!!

I agree with Alex though.  I will use properties so that way I can always validate the values that are passed into the variables.

Thanks a lot for your help Gentlemen!  (I hope I didn't just offend someone, but there really aren't many ladies among us)

axnst2
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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