Link to home
Start Free TrialLog in
Avatar of GiedriusS
GiedriusS

asked on

References

hi,
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
Giedrius
Avatar of Razzie_
Razzie_

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.

HTH,

Razzie
Avatar of GiedriusS

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Razzie_
Razzie_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial