how to pass a "this" pointer in C#

Sure this is easy but Im stuck  Ive migrated from C++ to C# and cant seem to pass the "this" pointer to an object.  heres the setup

theres form1 derrived off Form, in form1 I create 4 objects, each object basically sets up information supplied by a user so a chart can be created thats it.  When the user is finished in the object they press a button to build a chart.

I have two scenarios, either I return somehow to form1 and create the charts there as I can see the created objects anyhow from form1, and that is my prefered solution, or I can build the charts from within each object.  Whatever I think of as a solution Im left with sending in a "this" pointer to the objects as a paramerer to the constructor when I am creating them.  So whateve route I take I need "access" to form1.  I can get the "this" pointer in no probs but I cant create a "type" Form1 I can only create a "type" Form, ie the baseclass.  

I cant fathom how to see the type Form1 so I can return via a function call to form 1 when I click the button.  Ive messed with the using directives but I cant see the form1 namespace for form1 no matter what I try and do within the object class.

There are 2 projects each with a different namespace, form1 is dependent on the object class.  I really dont care what the settings need to be like as long as I can see form1 or you say, NO dont do it like that , do it like this?  How do I proceed, cheers
TheMoogAsked:
Who is Participating?
 
keustermansConnect With a Mentor Commented:
If I understand you right you are attempting to pass an instance of form1 to another object so that you can access some data on the form in the other object.

Do the follwing
Create an Interface that will expose some methods on class form1 that will allow you to get the required data and make form1 implement that interface.

The constructor or method of the receivng object will have to have a parameter of Type (Your interface name) ;this will allow you to access data on form1

See the code snippet for and example of how this is done using the MVC pattern.

You will notice that the controller constructor accepts IView as a parameter wich allows the controller to manipulate objects in form1. In this case the content of a textbox is set, you can however in the same way extract information form the form by exposing the correct methods throught the interface.

Hope this helps.


in the receiving object
    public partial class Form1 : Form,IView
    {
        CalcController calcController;   
        public Form1()
        {
            InitializeComponent();
            calcController = new CalcController(this);
        }
        public void SetValue(Decimal answer)
        {
            textBox3.Text = answer.ToString();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            calcController.Add(Decimal.Parse(textBox1.Text), Decimal.Parse(textBox2.Text));
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            calcController.Subtract(Decimal.Parse(textBox1.Text), Decimal.Parse(textBox2.Text));
        }
    }
 
    internal interface IView
    {
        void SetValue(decimal answer);
    }
 
    internal class CalcController
    {
        private IView iView;
        private CalcModel calcModel;
        internal CalcController(IView view)
        {
            calcModel = new CalcModel();
            iView = view;
        }
 
        internal void Add(decimal a, decimal b)
        {
            iView.SetValue(calcModel.Add(a,b));
        }
 
        internal void Subtract(decimal a, decimal b)
        {
            iView.SetValue(calcModel.Subtract(a,b));
        }
    }
 
    internal class CalcModel
    {
        internal decimal Add(decimal a, decimal b)
        {
            return a + b;
        }
 
        internal decimal Subtract(decimal a, decimal b)
        {
            return a - b;
        }
    }

Open in new window

0
 
daveamourCommented:
Ok this is a very difficult question to understand just from the text but I'm sure I would be able to help.
Would you be able to construct a bare bones solution to illustrate your issue so that I can download that and have a look?
0
 
TheMoogAuthor Commented:
in simpler terms, now do I see the "type" of the main form (in this case its called Form1 derrived from Form by C# not by me when I created the project) if I send "this" into the constructor of a new class I created, thats really the question?

The reason I need the type is so I can set "this" as a class member variable in the new class.  I dont know how to see the "Form1 type" I can only see the "Form type".  Cheers
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
daveamourCommented:
Ok well I think you just need to cast it then providing there are no other issues
((YourFormClass)z).YourMethod()
Where YourFormClass is the class whcih "this" is supposed to be. z is the parameter name in the constructor or wherever you are passing this.
Make sense?
 
0
 
daveamourCommented:
PS - Can I see the signature of your constructor where you pass this in?
0
 
TheMoogAuthor Commented:
Thanks I understand what to do now cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.