Sharing data

  • Thread starter Thread starter milop
  • Start date Start date
M

milop

Ok, I've been working in ASP.net for years, so I've forgotten Windows Forms
programming.

Where do I store data that needs to be accessed later on in a different
form?

In an oversimplified example, form1 goes out to a database and gets a
dataset. Then the user clicks "Continue" and form2 displays. I need form2 to
access the dataset retrieved in form1.

Is there a "global" memory area in Winforms that resembles "Session" in
ASP.Net?

Thanks in advance,

Mike
 
Hello Milop,
Ok, I've been working in ASP.net for years, so I've forgotten Windows
Forms programming.

Where do I store data that needs to be accessed later on in a
different form?

In an oversimplified example, form1 goes out to a database and gets a
dataset. Then the user clicks "Continue" and form2 displays. I need
form2 to access the dataset retrieved in form1.

Is there a "global" memory area in Winforms that resembles "Session"
in ASP.Net?

Any static variable will do... as long as you only need one set of the data
to stay around.

To get something that mostly resembles the Session, you could put a hastbale
as a static variable in your Program.cs:

public static Hashtable Session = new HashTable();

A better way would be to implement a singleton class which holds the data
you want to save.
 
Hello Milop,
Ok, I've been working in ASP.net for years, so I've forgotten Windows
Forms programming.

Where do I store data that needs to be accessed later on in a
different form?

In an oversimplified example, form1 goes out to a database and gets a
dataset. Then the user clicks "Continue" and form2 displays. I need
form2 to access the dataset retrieved in form1.

Is there a "global" memory area in Winforms that resembles "Session"
in ASP.Net?

Any static variable will do... as long as you only need one set of the data
to stay around.

To get something that mostly resembles the Session, you could put a hastbale
as a static variable in your Program.cs:

public static Hashtable Session = new HashTable();

A better way would be to implement a singleton class which holds the data
you want to save.
 
Thanks for responding, Jesse.

You see, that's what I was unsure about. Whether to have a Main sub and
create new form objects, or use the forms as static classes.

I took the latter route (static classes) but am starting to think the former
(Main sub) is better.

What's your opinion?

Thanks again,

Mike
 
Thanks for responding, Jesse.

You see, that's what I was unsure about. Whether to have a Main sub and
create new form objects, or use the forms as static classes.

I took the latter route (static classes) but am starting to think the former
(Main sub) is better.

What's your opinion?

Thanks again,

Mike
 
A common pattern is to pass the item (your dataset) in through a constructor
of your form2 class, assuming Continue is on form1 and it creates and calls
form2. I find that tidier and more controlled than a static.
 
Hi, Phil. Thanks for the response.

What I wound up doing was creating a class that inherits ApplicationContext.
This class has the business objects that I need.

I then orchestrate the displaying of forms from there and, like you said,
pass the business object(s) into the constructors of each form, which in
turn populate that portion of the business object pertinent to the form's
data.

So then each form is essentially just a data collection vehicle and they
have no conception of the overall goal of the application. Particular events
in each form (like Next and Back button clicks, etc.) are raised by the form
and handled in my AppContext class. Simple input validation (like email
address structure) is performed in the form.

I find that the overall flow logic is easier to follow this way because it's
all in one spot.

I started coding logic in each page and quickly it reminded me of my old
Foxpro Windows days. The application's logic was split into discrete little
chunks of code spread throughout the app. It made debugging a real pain.

What do you think of this approach? What are the drawbacks?

Thanks again,

Mike
 
Back
Top