[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

References

Posted on 2004-10-27
3
Medium Priority
?
187 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:GiedriusS
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 12422284
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
0
 

Author Comment

by:GiedriusS
ID: 12430318
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?
0
 
LVL 8

Accepted Solution

by:
Razzie_ earned 450 total points
ID: 12431624
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.

HTH,

Razzie
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

607 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