FP2007

Hi, I'm new to C# and newish to OOP. I'm a little confused about the best way of dealing with "global" data.

For historical reasons my WinForms application doesn't use a database instead it uses a folder with various text and binary files in it. Some of these files are read-in at program startup and others are read on demand.

At the moment I have an object called dataArchive that contains various collections of objects read from these data files. Ideally I'd like this dataArchive class to be accessible from every form without having to constantly be passing it around.

So firstly what's the best way to achieve this. Secondly is this the right way to handle this situation, whenever I've searched for information most people seem to disapprove of having global data at all in OOP programs, so what is the proper alternative

Thanks for any answers you can give me.


Re: Visual C# General Best practice for global data

TilakGopi

At the moment I have an object called dataArchive that contains various collections of objects read from these data files. Ideally I'd like this dataArchive class to be accessible from every form without having to constantly be passing it around.

So firstly what's the best way to achieve this.

Hi ,

To achieve this make the dataArchive as public static variable of it's parentForm such that it can be accessible from every form without having to constantly be passing it around.

AFAIK, it's one good solution.Other better solutions may exist.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General Best practice for global data

Matthew Watson

I'd personally put global program data into static properties of the Program class. (The Program class is a static class that is created by the IDE for each new project, and it contains the application's Main() method too.)




Re: Visual C# General Best practice for global data

ThE_lOtUs

First, try to not use global object as much as possible.

Now, this is what I do in .NET 1.x (don't know if there's a beter way in 2.0). It's call singleton. I rather have 1 shared instance then a bunch of shared object.

 

(it was converted from vb.net, don't know if it compile)

 

public class Global
{
 private static Mutex _mutex = new Mutex();
 private static Global _instance = null;
 private UserSecurity _security = null;

 public static Global Instance {
   get {
     _mutex.WaitOne();
     try {
       if (_instance == null) {
         _instance = new Global();
       }
     } finally {
       _mutex.ReleaseMutex();
     }
     return _instance;
   }
 }

 public UserSecurity Security {
   get {
            // Start of critical section
            // SyncLock _security
     if (_security == null) {
       _security = new UserSecurity();
       _security.Initialize();
     }
            // End SyncLock
            // End of critical section
     return _security;
   }
 }
}

 

// To access

Global.Instance.Security






Re: Visual C# General Best practice for global data

FP2007

Thanks for the suggestions I'm currently using TilakGopi's solution but I intend to experiment with the others as well.