Passing Object from form to form.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geebanga88
    New Member
    • Aug 2007
    • 62

    #1

    Passing Object from form to form.

    HI i have a main form which creates a studentDB object, (not acutally a database but an array). I also have a button which creates a new form "addStudentReco rd".
    Code:
     
    public partial class Main : Form
        {
            public StudentDB PrimarySchool;
            public Main()
            {
                InitializeComponent();
                PrimarySchool = new StudentDB("Primary School"); 
            }  
            private void toolStripAddButton_Click(object sender, EventArgs e)
            {
                if (!AddStudentRecord.IsOpen)
                {
                    AddStudentRecord addRecord = new AddStudentRecord(ref     PrimarySchool);
                    addRecord.Show();
                    
                } 
            }
    As you can see i pass the studentDB object PrimarySchool as a ref to the record. My AddstudentRecor d code has an add button which calls a mehtod: PrimarySchool.A ddStudent(strin g Name) to add the student to the array.

    Code:
    public partial class AddStudentRecord : Form
        {
            public AddStudentRecord(ref StudentDB PrimarySchool)
            {
                InitializeComponent(); 
            } 
            private void AddStudentButton_Click(object sender, EventArgs e)
            {  
                if (!PrimarySchool.Addstudent(NameTextBox.Text)) // PrimarySchool does not exist in current context?
                {
                    MessageBox.Show("Student could not be added");
                }
                else
                {
                    MessageBox.Show("Student added");
                }
             }
    However i get an error saying PrimarySchool does not exist in the current context. And PrimarySchool only comes up on the intellisense inside the default constructor of the form? So my question is how do i get the object to be recognised in the onclickEvent of addStudentButto n? Or is ther a better way to passed the object to the add student form so it can be accessed by any event.
  • geebanga88
    New Member
    • Aug 2007
    • 62

    #2
    Didnt add a created local object in constructor. Solved it:)

    Comment

    Working...