?
Solved

Global variables in C#

Posted on 2006-06-20
8
Medium Priority
?
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 total points
ID: 16943042
Oops, sorry, this is C++ syntax. Should be:

A.x = 0;

int m = A.n;

0
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.

 

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
 
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

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

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