tags:

views:

73

answers:

2

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?

+8  A: 

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();
Justin
well I did try this but i get this error which is what brought me here:so thats why i thought you couldn't have objects in your static classesError 1 Inconsistent accessibility: return type 'MyNameSpace.MyClass' is less accessible than method 'MyNameSpace.StaticClass.GetObj()'
cferbs
In that case check the access modifier for MyClass. It needs to be public instead of private or internal.
John
@cferbs As Manfred said, make sure `MyClass` is declared as `public class MyClass`.
Justin
@Justin: Yes. A class needs to be public in order to be accessible from a different assembly. In the case of a nested class it needs to be public even in the case when used from within the same assembly.
John
and further a class used as return type in for a public method in a public class needs to be public as well
Rune FS
Yes that was the problem i forgot to make my object class public. thanks alot everyone.
cferbs
A: 

One option is to have a class with the accessors methods accessing a static object (or objects). The other parts of your system can use the class either as static or as a non-static. Here is the code:

  public class GlobalInformation {
     public static GlobalInformation CreateInstance() {
        // Factory method through GlobalInformmation.CreateInstance()
        return new GlobalInformation();
     }

     public GlobalInformation() {
        // Regular use through new GlobalInformation()
     }

     static GlobalInformation() {
        // Static initializer called once before class is used.
        // e.g. initialize values:
        _aString = "The string value";
     }

     public string AccessAString {
        get {
           return _aString;
        }
     }

     public Foo AccessAnObject() {
        return _anObject;
     }

     private static string _aString;
     private static readonly Foo _anObject = new Foo();
  }

Other parts of your system would use it as follows. Option 1:

var globalInfo = GlobalInformation.CreateInstance();
var aString = globalInfo.AssessAString;
var anObj = globalInfo.AccessAnObject();

Option 2:

var globalInfo = new GlobalInformation();
var aString = globalInfo.AssessAString;
var anObj = globalInfo.AccessAnObject();

Option 2 would be my preferred one (I'd remove the static factory method CreateInstance()) as you could change the implementation at any time including making (some of) the fields non-static. It would appear to be a regular class while sharing data.

John