Solved

Global variables in C#

Posted on 2006-06-20
8
433 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 250 total points
ID: 16943042
Oops, sorry, this is C++ syntax. Should be:

A.x = 0;

int m = A.n;

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

728 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