Newbie: Passing an object to a class
-
I am fairly new at programming; I am using Viusal Studio.net 2003. Here is the problem I am trying to solve: I have an MDI parent form which has a control. I also have a child form that needs to tell the control on the parent to do something. I seem to be able to pass the control by reference to the child form class, and if I try to manipulate the control while still in the class constructor, everything works fine. However, when I try to access it elsewhere in the child form, i get an "undeclared identifier" error. Does anyone know how to fix it? Here is some code to show what I am doing: THIS CODE IS IN THE PARENT FORM: private: System::Void button2_Click(System::Object * sender, System::EventArgs * e) { FormChild2 *formChild2 = new FormChild2(myControl); formChild2->MdiParent = this; formChild2->Show(); } THIS CODE IS IN THE FORMCHILD2 FORM: public __gc class FormChild2 : public System::Windows::Forms::Form { public: FormChild2(AxInterop::TWSLib::AxTws *myControl) { InitializeComponent(); myControl->connect("",7496,1); } }; SO while still in the class constructor I can use the "connect" property of the control, which I just passed by reference. However, I want to be able to use that control after a button_click event while in the FormChild2 form. But that is when I get the "undeclared identifier" error. Any suggestions would appreciated. Thanks, Jody Blau
-
I am fairly new at programming; I am using Viusal Studio.net 2003. Here is the problem I am trying to solve: I have an MDI parent form which has a control. I also have a child form that needs to tell the control on the parent to do something. I seem to be able to pass the control by reference to the child form class, and if I try to manipulate the control while still in the class constructor, everything works fine. However, when I try to access it elsewhere in the child form, i get an "undeclared identifier" error. Does anyone know how to fix it? Here is some code to show what I am doing: THIS CODE IS IN THE PARENT FORM: private: System::Void button2_Click(System::Object * sender, System::EventArgs * e) { FormChild2 *formChild2 = new FormChild2(myControl); formChild2->MdiParent = this; formChild2->Show(); } THIS CODE IS IN THE FORMCHILD2 FORM: public __gc class FormChild2 : public System::Windows::Forms::Form { public: FormChild2(AxInterop::TWSLib::AxTws *myControl) { InitializeComponent(); myControl->connect("",7496,1); } }; SO while still in the class constructor I can use the "connect" property of the control, which I just passed by reference. However, I want to be able to use that control after a button_click event while in the FormChild2 form. But that is when I get the "undeclared identifier" error. Any suggestions would appreciated. Thanks, Jody Blau
Hai Jody, the myControl in your child form is only the function scope. ie u can only use it in the Constructor because you paased it as an arguement. If you want to use the myControl in ChildForm, please declare a variable of AxTws in ChildForm and link it with Parent from //Parent Form private: System::Void button2_Click(System::Object * sender, System::EventArgs * e) { FormChild2 *formChild2 = new FormChild2(myControl); formChild2->MdiParent = this; fromChild2->myControl=this->myControl; formChild2->Show(); } //Child Form public __gc class FormChild2 : public System::Windows::Forms::Form { public: AxInterop::TWSLib::AxTws *myControl FormChild2() { InitializeComponent(); myControl->connect("",7496,1); } }; Now you can use the myControl from anywhere in the ChildFrom
-
Hai Jody, the myControl in your child form is only the function scope. ie u can only use it in the Constructor because you paased it as an arguement. If you want to use the myControl in ChildForm, please declare a variable of AxTws in ChildForm and link it with Parent from //Parent Form private: System::Void button2_Click(System::Object * sender, System::EventArgs * e) { FormChild2 *formChild2 = new FormChild2(myControl); formChild2->MdiParent = this; fromChild2->myControl=this->myControl; formChild2->Show(); } //Child Form public __gc class FormChild2 : public System::Windows::Forms::Form { public: AxInterop::TWSLib::AxTws *myControl FormChild2() { InitializeComponent(); myControl->connect("",7496,1); } }; Now you can use the myControl from anywhere in the ChildFrom