I'm going to wade in here, not because the question hasn't been answered, but because this is such a common problem for many programmers.
First, develop a naming convention for the form that controls the program. As Old Pendant states, this is the one that shares the EXE name. During development, I always call this form frmMain since it contains the Main() method. I tend to make frmMain() an MDI form and use it as the parent for all other (child) forms.
Second, following OOP practices, I don't like "global data". If you need a Demo object (using Handman's narrative), frmMain should have that object defined within frmMain, such as:
public class frmMain()
{
Demo myDemoObject = new Demo();
// more code
} // End frmMain code
Handman: You got the "doesn't exist in current context" because you forgot the parentheses after the word Demo at the end of the definition of myDemoObject. Always remember: the new keyword causes a call to the Windows Memory Manager to get enough memory to hold a Demo object. Assuming there is enough free memory, the subsequent call to Demo() calls the constructor to initialize that chunk of "Demo" memory to the default values for all data in the Demo object. That is, value types are initialized to zero and all reference types (e.g., strings) are initialized to null. The only reason to write your own constructor is when you're not happy with these default values.
Third, assuming you want to pass your myDemoObject to a different form named frmNextForm and assuming there's a button called btnNextForm, a simple way to do this is to add the following code to frmMain:
public class frmMain()
{
Demo myDemoObject = new Demo();
// more code
private void btnNextForm(object sender, EventArgs e)
{
frmNextForm frm = new frmNextForm(myDemoObject);
frm.ShowDialog();
}
} // End frmMain code
Note how we are passing your object, myDemoObject, to the next form at the time frmNextFrom's constructor is called. Keep in mind that myDemoObject is nothing but a memory reference, or pointer, to the myDemoObject data.
The code in the frmNextForm would look like:
public class frmNextForm()
{
Demo refDemoData; // Create a reference for a Demo object
// Definitons for other data in frmNextForm
public frmNextForm(Demo demoDataFromFrmMain) //Constructor
{
refDemoData = demoDataFromFrmMain; // Magic here!
} // Other methods in this class
}
Note that the memory reference from the Demo object in frmMain is assigned into refDemoData in the second form. However, because refDemoData now contains the same memory address as does myDemoObject in frmMain, anything you do to refDemoData is actually changing myDemoObject in frmMain because they both point to the same location in memory. This means that anything you do to refDemoData in the second form is permanently recorded in the myDemoObject in frmMain because they are, in fact, the same object.
While this is perhaps an H-bomb to kill an ant, read it 10 times and then write the code and single-step through that code and you'll see exactly what I mean.
I hope this helps rather than hinders. (If you have access to my book, check pp.388-394.)
Dr. Purdum
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|