Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We like to have a global instance of a class and would like to access across the application (in different forms etc)

What are the different possiblities? (other than static class).
Posted
Comments
Sandeep Mewara 11-May-11 10:20am    
Elaborate on why?

Only a static class will be accessible across all forms, unless you create an instance of a non-static class and pass the reference to that to all the forms you think are likely to need access.

However, nearly every C# program manages very nicely without this. What are you trying to achieve that you think needs a global class chock-full of variables?
 
Share this answer
 
Comments
Olivier Levrey 11-May-11 11:48am    
My 5.
Sergey Alexandrovich Kryukov 11-May-11 12:42pm    
"Only a static class will be..."
Griff, this is not 100% accurate. Replace "static class" with "class" -- it will formally be non-static but accessible as static. Moreover, for singleton behavior it's enough to have just one static instance or property which would provide access to a single instance.
--SA
Sergey Alexandrovich Kryukov 11-May-11 12:52pm    
Well, in fact, two static members. I illustrated my idea on singleton implementation.
Please see my answer.
--SA
If you really need global access to a single instance, use the Singleton pattern.
http://msdn.microsoft.com/en-us/library/ff650316.aspx[^].
 
Share this answer
 
Comments
Hemant__Sharma 11-May-11 10:33am    
But the singleton pattern uses "static".
Rick Shaub 11-May-11 11:46am    
Yes but it isn't a "static class", it's a static instance of a class. There's a difference.
Olivier Levrey 11-May-11 11:49am    
My 5.
Sergey Alexandrovich Kryukov 11-May-11 12:55pm    
Rick is right.
I actually explain this in my comment to Griff's answer (where I find some incorrect statement) and my sample code.

Please see my answer.

My 5 for Rick's answer.
--SA
This is one of the ways to implement singleton behavior without having a static class:

class Singleton {
    public Singleton() { } //prevent instantiation from outside
    public static Singleton Instance {
        get {
            if (SingleInstance == null) //keep it lazy
                SingleInstance = new Singleton();
            return SingleInstance;
        }
    }
    public int FirstProperty { get; set; }
    public string SecondProperty { get; set; }
    public void Run() { /*...*/ }
    //...
    private static Singleton SingleInstance;
}


All access if performed via static Singleton.Instance which guarantee both global access and uniqueness (withing Application Domain).

For static data it's very important to secure thread safety using locking, in case of more than one thread having access to the singleton.
Alternatively, volatile keyword can be used.
See: http://msdn.microsoft.com/en-us/library/x13ttww7(v=VS.100).aspx[^], http://www.dotnetperls.com/volatile[^].

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 11-May-11 12:57pm    
Nice reply, my 5
Sergey Alexandrovich Kryukov 11-May-11 13:00pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900