Is there an way to share data between all forms in my c# application?

A

Anjan

Hi! I want to learn if there is an way to share data between forms.

In my application i will have around 50 different types of forms. and i need
to have some data that is to be available in every form in my application.
Much like the session in c#, once set can access from any page in the asp
..net app.

can i do the same in c# winform application?
 
F

Frank Uray

Hi

You can declare as public and/or static.
I normaly make one public class with shared objects.
These objects are available in the complete solution.
If you declare as static, you dont have to create a instance,
the object is directly available.

regards
Frank
 
A

Anjan

Thanks for the reply. But that procedure works fine when your data does not
change or constant.

in my case say i have declared a variable "userName" which will change at
any point during the application life time.

But i want access to this "userName" variable globally from any form in my
application. Any solution for this scenario?

best regards
 
J

Jeff Johnson

Thanks for the reply. But that procedure works fine when your data does
not change or constant.

in my case say i have declared a variable "userName" which will change at
any point during the application life time.

But i want access to this "userName" variable globally from any form in my
application. Any solution for this scenario?

That's not the kind of "static" he was talking about. When you mark a class
static then only one instance of it exists, and you access its properties
through the class name, not a variable. Have you ever used File.Open() or
Path.Combine()? Those are static classes. When you create a static class you
can give it properties which are read/write. It's just that there is only
one instance, so if formA changes the MyStaticClass.Blah property, then when
formB reads that property it will see whatever value formA just set.
 
A

Anjan

Yes! That solves the problem!

Actually i have used static methods, but the use of static properties as
global variable just didn't occur to me :(

Have to be very attentive next time when reading articles or docs.

Many many thanks to Both Jeff Johnson and Frank Uray
 

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