Global Variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to know how to pass a variable from one Windows Form1 to Form2?

Coming from VB background, it was a simple matter of creating a module and
declaring the variable this made the variable availlable to the entire
project. What is the method used to achieve this in C#?
Please provide an example if you can. Thanks
 
I would like to know how to pass a variable from one Windows Form1 to Form2?

Coming from VB background, it was a simple matter of creating a module and
declaring the variable this made the variable availlable to the entire
project. What is the method used to achieve this in C#?
Please provide an example if you can. Thanks

Declare a public variable (or a private variable with a public
property) inside Form1. Form2 can read it since it is public.

rossum
 
You can declare a public variable in the Class Form1 and you can access this
in Form2 using the following code:

int k; // declare this as public in Form1 class

Now, in Form2,

Form1 f1 = new Form1();
f1.k=10;
int p = f1.k;
MessageBox.Show(p.ToString());
 

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

Back
Top