Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

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

Avatar of gmcorbe
gmcorbeFlag for United States of America asked on
.NET ProgrammingC#
4 Comments1 Solution822 ViewsLast Modified:
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();
	    }
    }     
}
// ========================================================================================
ASKER CERTIFIED SOLUTION
Avatar of iHadi
iHadiFlag of Syrian Arab Republic image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers