473,612 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 myClassToConfig ure ;

void CallADialog ()
{
DialogForm dlg = new DialogForm();
dlg.theClass =
(MySettingsClas s)myClassToConf igure.Clone() ; // make a copy for the
dialog

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

} // end MyBaseClass

Dec 21 '07 #1
2 1369
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********@goo glemail.comwrot e in message
news:fc******** *************** ***********@e25 g2000prg.google groups.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 myClassToConfig ure ;

void CallADialog ()
{
DialogForm dlg = new DialogForm();
dlg.theClass =
(MySettingsClas s)myClassToConf igure.Clone() ; // make a copy for the
dialog

if (dlg.ShowDialog () == DialogResult.OK )
{
myClassToConfig ure = 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
5459
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 box with a text box in it and the text box data will be returned to the label that was clicked. Don't laugh but this is the code I use, it's OK for three controls but gets complicated when using alot of controls and although it works I think its...
4
4863
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 parameter. For example, FormOptions formOptions = new FormOptions(); if (formOptions.ShowDialog(this) == DialogResult.OK) {
10
16201
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
3473
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 click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code and zipid are both passed to the lookup form/dialog by reference. I...
8
4406
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 click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code is passed to the lookup form/dialog by reference. I then load a...
2
1855
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 data on the various wizard pages I had to manually create hidden form fields and pass these between pages. The form action either went to the next or previous page in the dialog, depending on whether NEXT or BACK button was pressed. All validation...
6
1256
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
2470
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 the user It then dawned on me that I was passing in a readonly property into the method yet neither at compile time or runtime was I getting any kind of error or warning. I tested with a simple string field and it alter the string as expected but...
2
2250
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 = ("Primary", "priSec") print "tpl:", tpl self.emit(QtCore.SIGNAL("setLabel"), tpl)
0
8173
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8115
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8617
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8568
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6082
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4111
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2555
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1699
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.