473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to pass a reference itself

I want to pass an object to a dialog box, and have the dialog box work on the
object, so that when the dialog box closes, my program now sees the altered
object.

This turns out to be not as simple as passing a pointer in a native program.

How can I pass the object into the dialog box constructor, so that all of
the functions in the dialog box class will act upon the original object?

I tried passing the object with the "ref" parameter, but this seems to have
effect only within the immediate function being called. It doesn't have
effect over the whole dialog box class (of course.)

Thank you,
Rich
Mar 16 '06 #1
5 3979
Assuming your dialog box isn't itself creating a new/different instance,
this /should/ work just fine; the real question is: what is your dialog
doing (perhaps wrongly). The way to do this is something like:

public class MyDialog : Form {
private readonly MyDataClass instance;
public MyDialog() {} // need default ctor for designer - plus all the
initialise component jazz
public MyDialog(MyDataClass instance) : MyDialog() { // no need for ref
unless the ctor reassignes the parameter
this.instance = instance;
}
}
(you could also use a property getter/setter around "instance" instead of
the ctor)

Now - the important thing is what your dialog does; what it /should/ do
(assuming we want immediate updates) is update the underlying instance:

private void SomeButton_Click(object sender, EventArgs e) { // typically
an apply button
instance.SomeProperty = true; // or some such manipulation; typically
reading values from contained controls
}

Having done this, we have updated the underlying object. So does this
approach not work for you? Note that if you are using data-binding, then
your object must support either the INotifyPropertyChanged interface (in
2.0), else the {PropertyName}Changed pattern (1.1) for the original UI to be
notified of the updates.

Does this help any?

Marc
Mar 16 '06 #2
second ctor should read ": this()", not ": MyDialog()"; the perils of
notepad coding...

Marc
Mar 16 '06 #3
And a final thought; things will work *very* differently if you have a
struct instead of a class; class would be normal for this usage.

If you are using a struct (for whatever reason), then the property getter /
setter is the way to go - i.e. you set it before showing the UI (creating a
clone in the process), update it (the clone) in the UI, and then (after
closing) read the value from the getter (creating a *3rd* instance),
assigning that back to your original variable. Like I say : class would be
normal usage here!

Marc
Mar 16 '06 #4
Hi Marc,

Thank you for your very thoughtful reply. I can't explain exactly why I was
having trouble, but for some reason, when the dialog box returned from
ShowDialog, the original instance was not updated to reflect changes from the
dialog box.

I solved the problem by putting a method into the dialog class that simply
returns the object instance inside the box, so that before the box is
disposed, I am retrieving it.

But the next time I implement a dialog box, I will definitely do it the way
you describe.

Thanks again,
Rich

"Marc Gravell" wrote:
And a final thought; things will work *very* differently if you have a
struct instead of a class; class would be normal for this usage.

If you are using a struct (for whatever reason), then the property getter /
setter is the way to go - i.e. you set it before showing the UI (creating a
clone in the process), update it (the clone) in the UI, and then (after
closing) read the value from the getter (creating a *3rd* instance),
assigning that back to your original variable. Like I say : class would be
normal usage here!

Marc

Mar 16 '06 #5
vj
This is true... the Object will be lost, even tho it is by ref... as the
dialog box will disposed. You have to take it out, before you dispose the
dialog box..,
VJ

"Rich S." <Ri***@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
Hi Marc,

Thank you for your very thoughtful reply. I can't explain exactly why I
was
having trouble, but for some reason, when the dialog box returned from
ShowDialog, the original instance was not updated to reflect changes from
the
dialog box.

I solved the problem by putting a method into the dialog class that simply
returns the object instance inside the box, so that before the box is
disposed, I am retrieving it.

But the next time I implement a dialog box, I will definitely do it the
way
you describe.

Thanks again,
Rich

"Marc Gravell" wrote:
And a final thought; things will work *very* differently if you have a
struct instead of a class; class would be normal for this usage.

If you are using a struct (for whatever reason), then the property getter
/
setter is the way to go - i.e. you set it before showing the UI (creating
a
clone in the process), update it (the clone) in the UI, and then (after
closing) read the value from the getter (creating a *3rd* instance),
assigning that back to your original variable. Like I say : class would
be
normal usage here!

Marc

Mar 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
11
by: Vanessa | last post by:
Hi, I would like to know whether there's any way for me to pass an object by reference to another form? Regards Vanessa
14
by: dumboo | last post by:
hi there i m little bit confused over the following problem, i have understood wt the following code is doing...but not able to get the actual techinical stuff.....i have had a lot of hot debate...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Alvin Bruney | last post by:
The more knowledgable I get about this .net world, the more questions I have. ..NET uses pass by reference for all objects....uhhh I mean pass by value. (Couldn't resist this jab) Consider a...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.