473,414 Members | 1,716 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,414 software developers and data experts.

Passing Class to Dialog

I have a dialog box (form) that configures items in a class. I'm
wanting make sure that I handle OK and Cancel properly and that I also
manage memory (I hate the fact the C# supposedly does this for me).
Nonetheless, my question is regarding cloning. I'm assuming that I
have to clone my object into the dialog so that I can corrupt it in
the dialog and still hit "Cancel". Do I then need to clone it back
into the calling object when the user hits "OK" or can I just do a
"copy" (with the "=" operator)?

public class MyBaseClass
{
MySettingsClass myClassToConfigure ;

void CallADialog ()
{
DialogForm dlg = new DialogForm();
dlg.theClass =
(MySettingsClass)myClassToConfigure.Clone() ; // make a copy for the
dialog

if (dlg.ShowDialog() == DialogResult.OK)
{
myClassToConfigure = dlg.theClass ; // or should
I "Clone" it back?
}
} // end CallADialog

} // end MyBaseClass

Dec 21 '07 #1
2 1363
Do I then need to clone it back
into the calling object when the user hits "OK" or can I just do a
"copy" (with the "=" operator)?
Well, "=" is only "copy" if we are talking about a struct; if this is
a class, then "=" simply assigns your variable to the existing
instance. In most cases, this should be fine.

However; due to some ambiguous naming a few years ago, it isn't
entirely clear whether Clone() means "deep clone" or "shallow clone",
so even in the "cancel" scenario it is possible that sub-components of
your class remain corrupted.

You could look at the "memento" pattern here; alternatively, for
simple data structures perhaps consider serializing/deserializing the
object (to binary or xml) to ensure you have a completely standalone
deep clone.

If your data is simple - pick a simple strategy. If you have complex
data then unfortunately you sometimes need complex code to handle
"undo" or "apply changes to existing object" scenarios.

Marc
Dec 21 '07 #2
Hi,

It does depend of your object in question. Most of the time you pass your
instance to the dialog, onthe Load event the dialog copy those values to its
controls. then only when pressed Ok the dialog copies back the values to the
instance.

In this way you do not need to keep a copy of the original values.

In the weird case that the above cannot be applied take a look at
Serialization.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"mcmalburg" <mc********@googlemail.comwrote in message
news:fc**********************************@e25g2000 prg.googlegroups.com...
>I have a dialog box (form) that configures items in a class. I'm
wanting make sure that I handle OK and Cancel properly and that I also
manage memory (I hate the fact the C# supposedly does this for me).
Nonetheless, my question is regarding cloning. I'm assuming that I
have to clone my object into the dialog so that I can corrupt it in
the dialog and still hit "Cancel". Do I then need to clone it back
into the calling object when the user hits "OK" or can I just do a
"copy" (with the "=" operator)?

public class MyBaseClass
{
MySettingsClass myClassToConfigure ;

void CallADialog ()
{
DialogForm dlg = new DialogForm();
dlg.theClass =
(MySettingsClass)myClassToConfigure.Clone() ; // make a copy for the
dialog

if (dlg.ShowDialog() == DialogResult.OK)
{
myClassToConfigure = dlg.theClass ; // or should
I "Clone" it back?
}
} // end CallADialog

} // end MyBaseClass

Dec 21 '07 #3

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

Similar topics

1
by: Prosonman | last post by:
Hi, What is the best way to transfer variables to and from a dialog box? My project consists of a form with a number of controls, lets say three Labels, when a label is clicked it opens a dialog...
4
by: Ron Rohrssen | last post by:
I want to show a dialog and when the form (dialog) is closed, return to the calling form. The calling form should then be able to pass the child form to another object with the form as a...
10
by: Steve | last post by:
Can anyone recommend the best way to pass a string back to the calling class (windows form) from a dialog that was shown modally using ShowDialog?
11
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
2
by: Glenn | last post by:
I am an old ASP developer that has begun porting existing apps to ASP.NET. I am currently working on a wizard dialog that contains about 4 pages. In ASP in order to keep track of user entered...
6
by: John | last post by:
Hi How can I pass a string from a form to a dialog which I am trying to open from the same form? Thanks Regards
7
by: DareDevil | last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by...
2
by: Pradnyesh Sawant | last post by:
Hello, I have a pyqt4 code in which i'm trying the signal/slot mechanism. The (stripped) code is as follows: class D(QtCore.QThread): def __init__(self): QtCore.QThread.__init__(self) tpl =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.