Avatar of gmcorbe
gmcorbeFlag for United States of America asked on

C# - Need to Communicate from Class Instance to the "Parent" Form Object File That Created It

I have created a simple C# Project.  It contains one form (Form1.cs)  and one class (doWork.cs)
file.  In Form1, I create an instance of the class.  I call a property in the class to set an object
reference back to the main form (Form1).  From a private method in the doWork class, I would
like to call a Public method in the form that created the class (Form1).
The code has been copyed from each file and is displayed below:
THANKS.
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
// START of Form1.cs  object  =======================================================
namespace PtrExample
{
    public partial class Form1 : Form
    {
        doWork MyDoWorkClass = new doWork();
 
        public Form1()
        {
            // allow class instance object to "point back and call methods in this "parent" object
            MyDoWorkClass.PointerToParent = this;
 
            InitializeComponent();
        }
 
        public void ExecuteClassRequest()
        {
            MessageBox.Show("I'M HERE", "NOTICE:");
        }
    }
}
// ========================================================================================
 
 
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
// START of doWork.cs  object  =======================================================
namespace PtrExample
{
    class doWork
    {
        private object mPointerToParent;
 
    
        // property
        public object PointerToParent
        {
           // get { return mPointerToParent; }
           set { mPointerToParent = value; }
        }
 
        // method
 
        private void CallMethodInParent
        {
            // Would like to call method in "parent" that created this class!!!!!!!!
	        // FAILS HERE
	        mPointerToParent.ExecuteClassRequest();
	    }
    }     
}
// ========================================================================================

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
gmcorbe

8/22/2022 - Mon
Bob Learned

That kind of circular reference between two objects is dangerous, and not recommended.  If you have a UI element, and you want to have something come back from the class, I would use an event (delegate).
MogalManic

What you are describing is the Model View Controller pattern(MVC), or the simpler View Controller pattern.  Here is an example of MVC:
  http://www.codeproject.com/KB/cs/model_view_controller.aspx
ASKER CERTIFIED SOLUTION
iHadi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
gmcorbe

Your solution was perfect.  THANKS.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes