Solved

Global variables in C#

Posted on 2006-06-20
8
432 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

732 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