There are a few things here:
1) Which instance of VAR do you want to access - there can be several.
2) Why should Form1 let Form2 access it's variables.
To access something on Form1 from Form2 you need two things: the instance of Form1 that holds the variable you are concerned with, and for Form1 to deliberately expose it so that you can access it. I'll deal with these in reverse order.
The simple way to expose it is to declare it as
public
:
public string myString = "Hello, I'm a sting in Form1";
But this has problems, and is not recommended because it ties the two forms together - you cannot change the way Form1 works internally without considering the effects on Form2 (and potentially Form3, Form4 and so on). This is against the principles of OOP and is generally thought of as a bad idea.
A better solution is to use a
public
property in Form1:
private string myString = "Hello, I'm a sting in Form1";
public string MyString
{
get { return myString; }
set {myString = value; }
}
which does much the same thing, but allows you to change the way the string is handled internally if you need to, and allow you to check values and so forth to ensure your code will work.
Instances are another thing all together. Probably, when you created your instance of Form2, you did it from Form1, or vice versa. If you create Form1 from Form2 and display it as a dialog, then you have the instance ready, and a property is the way to go, just as you do with a OpenFileDialog:
Form1 f = new Form1();
if (f.ShowDialog() == DialogResult.OK)
{
Console.WriteLine(f.MyString);
}
If not, then you have to pass the Form1 instance to Form2 when you create it.
Or better, signal an event in Form1 that Form2 subscribes to, and pass the information in the EventArgs - that way Form2 can decide if it wants the information.