Global Variables

Posted on 2004-11-21
Last Modified: 2010-04-15

I'm quite new to C#, and wanted a bit of advice really on best practice.

I come from a vb background, and am used to having a declarations module, where I can add any global contansts, global ADO connections etc that can be used by the whole program.

I'm having some difficulty doing the same thing in C#. I've tried including a declarations class with the same namespace as every other class. Here I can put global enumerations which works well, but I can't seem to put global variables here.

Basically I just want a place to delcare my global SQL Connection. Can anyone suggest a good approach?


Question by:JezzaKashel
    LVL 34

    Expert Comment

    Hi JezzaKashel,

    Something like this

    public class myGlobals
    private string sConString;

    public string conString {
     get {
       return sConString;
     set {
       sConString = Value;


    LVL 48

    Accepted Solution

    C# doesn't allow global variables, but you can use static class members, they provide the same functionality.

    public class GlobalData         // like VB module
        public static string connectionString;

        // other data

    public class AnyOtherClass
        void SomeFunction()
             // use GlobalData.connectionString here

    Author Comment

    Thanks for all the comments.

    Seem to remember static class members is the way forward, so will give it a go and see what happens



    Featured Post

    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.

    Join & Write a Comment

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    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…
    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…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    733 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

    23 Experts available now in Live!

    Get 1:1 Help Now