Caching objects between forms

  • Thread starter Brian Sokolnicki
  • Start date
B

Brian Sokolnicki

I'm trying to cache a user object and gain access to it from a mutlipe
forms.

I have created a base class that all forms will inherit from. In this base
class, I have a propery to a User class which I want to cache and reuse on
all other forms so that I don't need to retrieve the user data from every
screen.

For every form that is open, there is a OnLoad override in the base class
that will call the getuser method in the user class and this will check to
see if there's a cached object or not.

Is this possible?

I have most of the logic written but can't figure out how to accomplish
this.

Thanks.
 
P

Patrick Steele

I'm trying to cache a user object and gain access to it from a mutlipe
forms.

I have created a base class that all forms will inherit from. In this base
class, I have a propery to a User class which I want to cache and reuse on
all other forms so that I don't need to retrieve the user data from every
screen.

For every form that is open, there is a OnLoad override in the base class
that will call the getuser method in the user class and this will check to
see if there's a cached object or not.

Is this possible?

Create a static (Shared in VB.NET) reference to your user object on your
main form. Something like this (C#):

public class MainForm : Form
{
private static UserObject _currentUser;

public static UserObject CurrentUser
{
get { return _currentUser; }
}
}

Somewhere in "MainForm", the user object is initialized and loaded. Now
all other forms can access "MainForm.CurrentUser" to retrieve the one
user object.
 
B

Brian Sokolnicki

Thanks, sounds like it should work.

Patrick Steele said:
Create a static (Shared in VB.NET) reference to your user object on your
main form. Something like this (C#):

public class MainForm : Form
{
private static UserObject _currentUser;

public static UserObject CurrentUser
{
get { return _currentUser; }
}
}

Somewhere in "MainForm", the user object is initialized and loaded. Now
all other forms can access "MainForm.CurrentUser" to retrieve the one
user object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top