Global instance of a class

I have a class defined in a separate file (general.cs).

Where I have 2 declare the instance of this class in order to have a global object

I tried 2 declare it in Program.cs or in form1.cs, but I can't see the object in any other form ...



Answer this question

Global instance of a class

  • Yonglun Li

    As ilyas has suggested, the best thing to do here is to make you class static! so without creating any instance of it every class in your application can call it's fucntions and access its data!

    This would be my choice If i would need to do something like you are trying to do!

    Think Again for the Right Choice

    Best Regards,



  • R.Tutus

    if you need more than one instance of a class then you would need to put it in an array for example, such as a strong typed generic list, then either make that static so you can access it from anywhere, remove or add items, or pass around the collection from one class to another.



  • ArcPadNewbie

    But if I need more than one instance of that class
  • Shyamal Patel

    Then First of all dont use static and leave it as its what else to do is make a public class with Dictionary Collection and Key With Unique ID, and Value as That class's Object

    Now whever you create an instance of that class put that class's Reference in that Collection in other helping class, Soother classes can use! And you can delete that from that collection whenever needed. Remeber to make safe class to that Objects, See then you have 10 Threades trying to play with the same sgared instance simultainously! So there should be some loginc toprevent them to mess up!

    This solution is for you and I hope You understand what I mean!

    Need more on that Feel Free to put next question here!

    Best Regards,



  • RyanB88

    Virgil Rucsandescu wrote:

    OMG, tons of problems ....

    For the moment I need only one object, so I made the "TableConfiguration" class static.

    I can access this class from anywhere, but I can't access any of its fields, even all of them are public .... :-((

    LOL !

    Yes, As Ilyas Mentioned, You need to make all fields static too!

    Best Regards,



  • dg2007

    you would really be looking at making it static if you are trying to access it from different classes, or pass that reference around classes via the constructor of the class, setting it to a private property, then accessing it anytime within that class, since the reference would be stored in a private global variable of that class

  • Lawrex

    OMG, tons of problems ....

    For the moment I need only one object, so I made the "TableConfiguration" class static.

    I can access this class from anywhere, but I can't access any of its fields, even all of them are public .... :-((


  • TechSupportV

    because you also have to make them static, the fields/properties/methods of a static class must be static. you only declare private/public (the way you are thinking) when you create an instance of a class :-)

  • Global instance of a class