Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Static Class with Objects

Tags:

c#

oop

static

Ok, so I know you can't have objects in a static class but i need a class that i can hold objects that are accessible from different classes. I am making a dll that will provide extended functionality to another program so i can't just inherit or pass classes around either. if need be i can just maybe make the properties of each object i need to be in the static class which would work but not be as friendly as i would like. anyone have any other ideas on how to accomplish something like this?

like image 928
Cory Avatar asked Aug 03 '10 19:08

Cory


People also ask

Are static classes global?

Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.

Can C++ have static objects?

C++ also supports static objects.

Can we call static member function of a class using object of a class?

A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::. A static member function can only access static data member, other static member functions and any other functions from outside the class.

Can a global variable be static C++?

Static VariablesA static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static.


1 Answers

Actually, you can have objects in a static class -- they just have to be static objects.

For instance:

public static class SharedObjects
{
    private static MyClass obj = new MyClass();
    public static MyClass GetObj() 
    {
        return obj;
    }
}

And from elsewhere in your program you can call instance methods/properties/etc.:

SharedObjects.GetObj().MyInstanceMethod();
like image 79
Justin Avatar answered Oct 02 '22 13:10

Justin