Hi Guys.

I've been searching the web for a solution but it seams as though I have to ask a question....LOL

I have a couple of Projects consisting of a fex apps and Dll's all in one solution. One of my Classes is Called GlobalVariables which is referenced in all the apps and dll's and coded like this:

namespace GlobalVariables
{
    public class GlobalVariables
    {
        private static GlobalVariables TheSingleGlobalVariableInstance = null;

        public GlobalVariables()
        {
        }
           
        public static GlobalVariables GetGlobalVariables()
        {
            if (TheSingleGlobalVariableInstance == null)
            {
                TheSingleGlobalVariableInstance = new GlobalVariables();
            }
            return TheSingleGlobalVariableInstance;
        }

        public static String ADOConDMU = "Data Source = \\Program Files\\DMU\\Database\\HDDR8_BASIC_DMU.sdf ; Password = lassa";
        public static System.Data.SqlServerCe.SqlCeConnection conn = new SqlCeConnection("");
        public static System.Data.SqlServerCe.SqlCeCommand cmd = new SqlCeCommand();
        public static int Result4, Result6 = 0;
        public static Assembly DMUAssembly;
        public static Type Mytype;
        public static MethodInfo Mymethod;
        public static Object obj;
    }
}

I've put a simple code snipet in all of the apps and dll's to look at the variable "Result6" and write it out, but I change the value of Result6 every 10sec in my main app...Here is the snipet in the main app:

private void timer1_Tick(object sender, EventArgs e)
        {
            GlobalVariables.GlobalVariables.Result6 = GlobalVariables.GlobalVariables.Result6 + 10;
            Console.WriteLine("This is the DMUServer : {0}", GlobalVariables.GlobalVariables.Result6);
        }

and in everything else

void timer_Tick(object sender, EventArgs e)
        {
            DateTime mm = DateTime.Now;
            Console.WriteLine("This is the TestComms : {0} at {1}", GlobalVariables.GlobalVariables.Result6, mm);
        }

Now, my problem is that all the Dll's can see and write out the right value ("Each time it is changed"), but the EXE's(Other Apps) seams to be making a new instance of GlobalVariables and doesn't seam to see the changes value.....

Some one please help....I don't see what I'm doing wrong...


Thanx In advance.

Recommended Answers

All 10 Replies

try making the class static as well:
public static class GlobalVariables

you are mixing static with singleton here

try this way

public class GlobalVariables
    {
        private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();

        private GlobalVariables()
        {
        }
           
        public static GlobalVariables  Instance
        {
get
{
            return TheSingleGlobalVariableInstance;
}
        }

private int result6;
 public int Result6
{
   get{ return result6; }
   set {result6 = value; }
}

and from your other code, you can call it like this

Console.WriteLine("This is the DMUServer : {0}", GlobalVariables.Instance.Result6);

I'm still learning much about C#.
I don't like globals very much, especially if they are a name followed by a number, but lets say this is not at hand here.
So what is wrong if I do it this way?

public class GlobalVariables
    {
        public GlobalVariables(){}

        private int result6;
        
        public int Result6
        {
            get { return result6; }
            set { result6 = value; }
        }
    }

And in my main code I would write :

GlobalVariables myGVars = new GlobalVariables();
            myGVars.Result6 = 3;
            Console.WriteLine("This is the DMUServer : {0}", myGVars.Result6);

The problem with that would be is that its not a singleton and can exist in multiple classes

private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();

        private GlobalVariables()
        {
        }
           
        public static GlobalVariables  Instance
        {
get
{
            return TheSingleGlobalVariableInstance;
}
        }

The above piece allows only a single instance to exist in the application, which can be called by any class without creating an instance of it

//you can do this
GlobalVariables.Instance.Result6 = 5;

//you can't do this
GlobalVariables gv = new GlobalVariables();
gv.Result6 = 5;

you normally would use this singleton method for a configuration accessor class or a group of data that needs to be shared throught the app without passing a class object around

commented: good explanation +4

dickersonka,
Interesting approach. Never had to do that.
A static class can not have a constructor, therefore it is automatically a global singleton by nature of the beast.

I have a static class in a very large plugin based application. All of the plugins (dll files), and the main app have access to it without any instantiation. All of the classes can change properties, run its methods, etc. with no problem. Just interested in your suggested approach, and what the pros and cons are over a static class.

// Jerry

when using this

public static int Result4, Result6 = 0;
 public static Assembly DMUAssembly;

there is no need for a singleton, they are static variables


when using this

public class GlobalVariables
    {
        private static readonly GlobalVariables TheSingleGlobalVariableInstance = new GlobalVariables();

        private GlobalVariables()
        {
                result6 = 5;
        }
           
        public static GlobalVariables  Instance
        {
get
{
            return TheSingleGlobalVariableInstance;
}
        }

private int result6;
 public int Result6
{
   get{ return result6; }
}

you have to access the variables through the instance of that class

also notice i took away the set public method in there, that allows these properties to be readonly from outside that class

depending upon the need, you may or may not want to do it that way

i should add to this, both really have their place

normally you would use these instance methods when they pertain to particular class, lets says a single point of reference for a connection string inside a configuration class

and for the static variables without instance you could use as formatting a string to remove all spaces

i sort of like to put them in the terms of Utility (static) vs Instance (Non utility)

Very intresting discussion here. Thanks to both of you I learned!

you normally would use this singleton method for a configuration accessor class

Hah! If only you knew the Hell I went through thanks to people thinking that.

or a group of data that needs to be shared throught the app without passing a class object around

This situation (the operative word being "needs") never exists.

If you ask me, the best use of singletons is to protect yourself from "junior" programmers who are liable to want to open up a fresh db connection in some inner function.

help for writing a program that check if password we enterd is right or not usibg assembly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.