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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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).
Avatar of MogalManic
MogalManic
Flag of United States of America image

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
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gmcorbe
gmcorbe
Flag of United States of America image

ASKER

Your solution was perfect.  THANKS.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo