Solved

Global variables in C#

Posted on 2006-06-20
8
428 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

22 Experts available now in Live!

Get 1:1 Help Now