Click here to Skip to main content
15,923,557 members
Home / Discussions / C#
   

C#

 
QuestionInteresting reference problem Pin
Rick van Woudenberg15-Jun-07 9:25
Rick van Woudenberg15-Jun-07 9:25 
AnswerRe: Interesting reference problem Pin
Luc Pattyn15-Jun-07 10:38
sitebuilderLuc Pattyn15-Jun-07 10:38 
AnswerRe: Interesting reference problem Pin
tarun suneja15-Jun-07 11:14
tarun suneja15-Jun-07 11:14 
AnswerRe: Interesting reference problem Pin
DavidNohejl15-Jun-07 11:26
DavidNohejl15-Jun-07 11:26 
AnswerRe: Interesting reference problem ( example ) Pin
Rick van Woudenberg15-Jun-07 14:13
Rick van Woudenberg15-Jun-07 14:13 
GeneralRe: Interesting reference problem ( example ) Pin
Luc Pattyn15-Jun-07 14:52
sitebuilderLuc Pattyn15-Jun-07 14:52 
GeneralRe: Interesting reference problem ( example ) Pin
Rick van Woudenberg16-Jun-07 1:01
Rick van Woudenberg16-Jun-07 1:01 
GeneralRe: reference problem [modified] Pin
Luc Pattyn16-Jun-07 1:29
sitebuilderLuc Pattyn16-Jun-07 1:29 
Hi Rick,

your "problem" is the standard issue of passing references around.

if a Form3 object has to operate on an existing Form2 object, it needs a reference to said
Form2 object.

There are many ways to get this done:

- pass the reference when constructing the Form3 object, in your case that could mean:
Form3 form3 = new Form3(this);   // inside Form2


This means Form3 needs a constructor that takes a Form2 and stores it in a
data member (your existing f2 could serve that purpose; a private one suffices).

- pass the reference by setting a public variable:
Form3 form3 = new Form3();   // inside Form2
form3.f2=this;
fomr3.Show();


using public data members (f2) is not recommended since now anyone can read/write them,
resulting in code that may become hard to understand and maintain.

- pass the reference by adding and using a public property:
Form3 form3 = new Form3();   // inside Form2
form3.F2=this;
fomr3.Show();


and inside Form3 add:
public Form2 F2 { set { f2=value;}}  // f2 itself can/should be private !


this is the recommended way: the property exists for a specific purpose, and since
it is public, it deserves a descriptive comment (triple slash).

Hope this helps.

Added:
whatever approach, you may want to test f2 against null before using it:
if (f2!=null) f2.Method();

And there are still other ways of acting on external objects; they would use events
and delegates.

-- modified at 7:36 Saturday 16th June, 2007


Questioncombobox Pin
topksharma198215-Jun-07 8:48
topksharma198215-Jun-07 8:48 
Questionsocket server Pin
topksharma198215-Jun-07 8:40
topksharma198215-Jun-07 8:40 
QuestionScrollable and Moveable Frames Pin
Syed Shahid Hussain15-Jun-07 7:58
Syed Shahid Hussain15-Jun-07 7:58 
AnswerRe: Scrollable and Moveable Frames Pin
Tarakeshwar Reddy15-Jun-07 8:15
professionalTarakeshwar Reddy15-Jun-07 8:15 
GeneralRe: Scrollable and Moveable Frames Pin
Syed Shahid Hussain15-Jun-07 14:58
Syed Shahid Hussain15-Jun-07 14:58 
GeneralA concepteual question Pin
Amar Chaudhary15-Jun-07 6:56
Amar Chaudhary15-Jun-07 6:56 
GeneralRe: A concepteual question Pin
bolhassanim@bellsouth.net15-Jun-07 7:12
professionalbolhassanim@bellsouth.net15-Jun-07 7:12 
GeneralRe: A concepteual question Pin
Shy Agam15-Jun-07 7:15
Shy Agam15-Jun-07 7:15 
GeneralRe: A concepteual question Pin
Amar Chaudhary15-Jun-07 7:25
Amar Chaudhary15-Jun-07 7:25 
QuestionNon-order-dependent deserialization of objects Pin
Shy Agam15-Jun-07 6:36
Shy Agam15-Jun-07 6:36 
QuestionMemory leak issue, memory management of unmanaged code Pin
logicaldna15-Jun-07 6:34
logicaldna15-Jun-07 6:34 
AnswerRe: Memory leak issue, memory management of unmanaged code Pin
Shy Agam15-Jun-07 6:39
Shy Agam15-Jun-07 6:39 
GeneralRe: Memory leak issue, memory management of unmanaged code Pin
logicaldna15-Jun-07 6:42
logicaldna15-Jun-07 6:42 
GeneralRe: Memory leak issue, memory management of unmanaged code Pin
Shy Agam15-Jun-07 7:00
Shy Agam15-Jun-07 7:00 
GeneralRe: Memory leak issue, memory management of unmanaged code Pin
logicaldna15-Jun-07 7:04
logicaldna15-Jun-07 7:04 
AnswerRe: Memory leak issue, memory management of unmanaged code Pin
Jimmanuel15-Jun-07 7:19
Jimmanuel15-Jun-07 7:19 
AnswerRe: Memory leak issue, memory management of unmanaged code Pin
Paul Conrad15-Jun-07 13:57
professionalPaul Conrad15-Jun-07 13:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.