Global Variables


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?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi JezzaKashel,

Something like this

public class myGlobals
private string sConString;

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


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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JezzaKashelAuthor Commented:
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


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.