?
Solved

Global Variables

Posted on 2004-11-21
3
Medium Priority
?
336 Views
Last Modified: 2010-04-15
Hi

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?

Thanks

Jeremy
0
Comment
Question by:JezzaKashel
3 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 12637855
Hi JezzaKashel,

Something like this

public class myGlobals
{
private string sConString;

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

Idea???

Dave
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 12637884
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
    }
}
0
 

Author Comment

by:JezzaKashel
ID: 12643750
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

Thanks

Jeremy
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 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