Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to reference an object across an application.

Status
Not open for further replies.

CrashDome

Technical User
Jan 7, 2002
86
US
I am writing an application such that much like a 'login' screen - a selection screen appears before the main program. The options available are in a database and once the option is selected by the user, an object is returned with the database values.

After the selection screen, I finally enter my main "Application.Run(<MDI form>);" context. Because it is an MDI form and there are many children AND dialog forms, I might need to reference that object quite often.

I initially created an inherited form for all my sub-forms that would upon loading reference the "Parent" or "Owner" form and retrieve the object. The more and more I get into it, the more and more I can't stand this method. I would really just like to call something like the configuration settings with something like [MyApp.CurrentSelection] and just get the object reference.

However, I am unsure on how to accomplish this efficiently?
 
perhaps you should use a class that holds a static array

Code:
namespace tracs_application
{
	/// <summary>
	/// Summary description for ConfigData.
	/// </summary>
	public class appComponents
	{
           private static string[] ConfigData;

           public static void setConfigDataArray(string[] theConfigData)
	   {
	        ConfigData= theConfigData; //move an array to the global array
	   }
   }
}

hope this helps

Age is a consequence of experience
 
If you only need one instance of this class, you will want to look at a singleton.

google singleton C#

You could address your information in the form of

MySingleton.Instance.Username from any class in the application.
 
Thank you both. I've never used singletons before and ended up going that route. Works great! On top of that since I have used static methods within the solution so I thought both of you deserved a star! ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top