[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to pass a "this" pointer in C#

Posted on 2009-02-22
6
Medium Priority
?
1,263 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:TheMoog
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 23708950
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
 

Author Comment

by:TheMoog
ID: 23709089
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
 
LVL 19

Expert Comment

by:daveamour
ID: 23709104
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 19

Expert Comment

by:daveamour
ID: 23709142
PS - Can I see the signature of your constructor where you pass this in?
0
 
LVL 7

Accepted Solution

by:
keustermans earned 200 total points
ID: 23709241
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
 

Author Closing Comment

by:TheMoog
ID: 31549979
Thanks I understand what to do now cheers
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question