Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win



Posted on 2004-10-27
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

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.



Author Comment

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?

Accepted Solution

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.



Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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