need reference to parent object
-
I have a Windows Form that creates an instance of a class of my own, and within that object I need to call a function that is part of the Windows Form. The problem is I don't know how to give my class a reference to my Windows Form. If this was C++ I'd just pass a pointer through the constructor... but this isn't C++... Anyone know the proper way to get a reference to a parent object so its methods can be called? Bungo!
-
I have a Windows Form that creates an instance of a class of my own, and within that object I need to call a function that is part of the Windows Form. The problem is I don't know how to give my class a reference to my Windows Form. If this was C++ I'd just pass a pointer through the constructor... but this isn't C++... Anyone know the proper way to get a reference to a parent object so its methods can be called? Bungo!
You can always pass the reference to the parent object and call methods of it. The reference is just like pointers in c++. Live Life King Size Alomgir Miah
-
I have a Windows Form that creates an instance of a class of my own, and within that object I need to call a function that is part of the Windows Form. The problem is I don't know how to give my class a reference to my Windows Form. If this was C++ I'd just pass a pointer through the constructor... but this isn't C++... Anyone know the proper way to get a reference to a parent object so its methods can be called? Bungo!
You could just pass the form to the class during instantiation using 'this' keyword. For example, assume your form is called MyForm and has a function called MyFormFunction that you want to call from MyClass: during instantiation in MyForm: MyClass newClass = new MyClass(this); in MyClass: private MyForm _externalForm; //declare as class level private variable //ctor public MyClass(MyForm passedFormReference) { _externalForm = passedFormReference; } public MyFormFunction() { _externalForm.MyFormFunction(); } Hope this helps, Regards, ----- Josh Lindenmuth jlindenmuth at paycepayroll dot com Payce Payroll - Smart for Business