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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Persisting objects in an application..

Status
Not open for further replies.

matthewking

Programmer
Jul 15, 2002
75
ES
Hi,

I have a CommsManager object in my application which manages network communications, I want to create a new instance of this when the application starts, then persist the object so I can access its methods anywhere in the application..

I think this is kind of like how a session in asp.net works, so I can do something like this:

CommsManager comms = Session["CommsManager"];
comms.DoSomething();

Please note, this is for an win32 app, not a web app. Session is just the closest thing I know to it.

Any help?

Thanks
Matt.

 
you need to instance CommsManager globaly (ie, within the namespace and class, but not within anything else. somewhere above the constructor).

CommsManager myComMan;

then you need to initialise it within the constructor.

myCommMan = new CommsManager();


this way of making an object is somehting you will do time and again. an object may be accessed from anywhere within the {} style braces in which it was declared. it must then (as a rule) be initialised from anywhere within it's scope before you can do anything else with it. the construcotr is the most common place to do this.

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
You can have an object persist in memory by declaring it &quot;static&quot;.

Additionally, you can call an object's methods without even instantiating an object by making its methods static.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top