Click here to Skip to main content
15,925,868 members
Home / Discussions / C#
   

C#

 
Questionsnippets Pin
tarun suneja15-Jun-07 12:51
tarun suneja15-Jun-07 12:51 
AnswerRe: snippets Pin
Luc Pattyn15-Jun-07 13:05
sitebuilderLuc Pattyn15-Jun-07 13:05 
QuestionDown-Casting Pin
Madmaximus15-Jun-07 12:12
Madmaximus15-Jun-07 12:12 
AnswerRe: Down-Casting Pin
Luc Pattyn15-Jun-07 12:19
sitebuilderLuc Pattyn15-Jun-07 12:19 
Questionevent form Pin
SVb.net15-Jun-07 11:55
SVb.net15-Jun-07 11:55 
AnswerRe: event form Pin
Luc Pattyn15-Jun-07 12:22
sitebuilderLuc Pattyn15-Jun-07 12:22 
GeneralRe: event form Pin
SVb.net16-Jun-07 22:01
SVb.net16-Jun-07 22:01 
GeneralRe: event form Pin
Luc Pattyn17-Jun-07 0:50
sitebuilderLuc Pattyn17-Jun-07 0:50 
QuestionKeystroke Re-mapping and Passthrough App Pin
thecodedemon15-Jun-07 11:18
thecodedemon15-Jun-07 11:18 
AnswerRe: Keystroke Re-mapping and Passthrough App Pin
Giorgi Dalakishvili15-Jun-07 11:48
mentorGiorgi Dalakishvili15-Jun-07 11:48 
QuestionFileSystemWatcher does not raise events Pin
AndrusM15-Jun-07 11:10
AndrusM15-Jun-07 11:10 
AnswerRe: FileSystemWatcher does not raise events Pin
Luc Pattyn15-Jun-07 11:50
sitebuilderLuc Pattyn15-Jun-07 11:50 
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 

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.