Hello,

Could you please give me some info about sharing data between forms?

I have a variable List<component> component_list; which "component" class is declared in a "Component.cs".
How to make that two forms see that class? (if I have to make something..)
And how to create "List<component> component_list" that it will be accessible ("read"/"write") from form1 and form2? Is that variable declared in the same "Component.cs" file? How to access it from each form?

Thanks in advance for you time!

Recommended Answers

All 2 Replies

Did'nt quite get your question 100%, but i guess you could use Static variables here.

Just create a class called Shared or something. Create a static variable in this class.

public static List<Components> comp_list = new List<Components>;

Now this variable 'comp_list' can be accessed from anywhere using the following:

'Shared.comp_list'

You can read/write from anywhere within the application.

you could instantiate it in a static class with a static property that refers to a static variable as mentioned above.

but another method would be just to instantiated it in the first form you need it in then expose it as a property of that form. then you could access it from anywhere either by passing the instance of the form to the other forms, or by using

(Form1)Application.OpenForms["Form1"]._component_list;

There are many ways really. These 2 are just the popular ones. depends on how many forms you are going to have and how often you need to access the data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.