Posted on 2004-10-27
Last Modified: 2010-04-15
i'm a c++ programmer and i have only basic skills of C#, so i hope you could help me.

I have two windows forms, the second one has an overloaded constructor, for example Form2(ref String sData), Form1 has a label, a user enters text in a textbox on second form and the entered text appears in the first form label.

One way is to get label by getting parent window, but this does not suite me, because i also need to change private member or even local variable..

The other way (it would be very simple in C++:)) I pass reference to a form constructor, then i save the reference in a class member, on some event (form close) i get that member (reference) and set the referenging object with the textbox value.
The question is how to save the reference wich i have in my constructor, and how to use it later in C#??

thanx in advance
Question by:GiedriusS
    LVL 8

    Expert Comment

    Well the easiest way to do that imho is to pass your form to the constructor of Form2:

    private Form f;

    public Form2(Form f)
       this.f = f;


    And when initalizing a new Form2 in your main form:

    Form f = new Form2(this);

    That way, you have a reference to your main form from Form2.



    Author Comment

    well thats not what i need.
    maybe you could answer the question:
    references can be used only when passing parameters to a function, or i can store a reference in some global variable?
    LVL 8

    Accepted Solution

    Ah ok I reread the question and I got it now, sorry was a bit late for me yesterday ;)

    For your new question: you cannot 'declare' a ref variable in your class, so yes you can only pass it to a function. What you can do however (I think it is what you want), is use a global variable and point it to the ref object in the function:

    private Form _f;

    public Form2(ref Form f)
       this._f = f;

    Now _f will be a reference to the object you passed to the function. Test it in a simple application if it suits your needs.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    Suggested Solutions

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles a…
    This video discusses moving either the default database or any database to a new volume.

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now