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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


GiedriusSAuthor Commented:
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?
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.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.