Often when you create a new class you need to provide some method for the new class to pass back information to the original class, or trigger events in the original class. You can have the original class check properties in the new class, but thats no good for events that get generated in the new class which need to callback.
In the original class
//***************************************
//***************************************
//********** CALLBACK FUNCTION **********
//***************************************
//***************************************
public: void MyCallbackFunction (MyClass ^%PassedClass)
{
ClassBeingWorkedOn = PassedClass;
}
//----- IN A FUNCTION THAT WANTS TO PASS THE CALLBACK FUNCTION -----
MyDelegateHandler ^del = gcnew MyDelegateHandler(this, &frmMain::MyCallbackFunction);
frmCellSetup ^ConfigForm = gcnew frmCellSetup(del, ClassBeingWorkedOn);
ConfigForm->ShowDialog();
//If your callback function was static you need to use this 1 argument version instead:
// MyDelegateHandler ^del = gcnew MyDelegateHandler(&this->MyCallbackFunction);
//and if so you will also need to ensure that anything it will modify are also static
Note that in this example you can ignore MyClass if you want – it an example of how to pass a class as part of a callback but you don’t need it if you don’t want to do this.
In the new class being created that will call back (i.e. a sub form)
//----- Add delegate definition in the new class namespace -----
namespace MyNamespace {
public delegate void MyDelegateHandler (MyClass ^%PassedClass);
//You generally don't need this, but if for some reason you need to declare the event inside a class (inside the 'public ref class'):
//public: event MyDelegateHandler ^del;
//*********************************
//*********************************
//********** CONSTRUCTOR **********
//*********************************
//*********************************
frmCellSetup(MyDelegateHandler ^del, MyClass ^%ClassBeingWorkedOn)
{
InitializeComponent();
ClassBeingWorkedOnLive = ClassBeingWorkedOn; //Store handle to class we're going to modify
SourceClassCallbackFunction = del; //Store callback fucntion
}
//GLOBAL DEFINITIONS
private: MyDelegateHandler ^SourceClassCallbackFunction;
private: MyClass ^ClassBeingWorkedOnLive;
//----- WHEN YOU WANT TO CALL THE CALLBACK FUNCTION -----
SourceClassCallbackFunction(ClassBeingWorkedOnLive);
If you need to have more than 1 other class call the source class callback function then simply create different MyDelegateHandler’s in each class.