Avoiding UpdateData

Modal Dialogs

You should never call UpdateData in a modal dialog. Just Don't Do It. Ever. There are many reasons. One reason is that the OnOK handler calls UpdateData to store the control values in the associated member variables. The OnCancel handler does not call UpdateData. Therefore, the assumed behavior, or the should-be-assumed behavior, is that if you set a collection of member variables before calling DoModal, upon successful completion of the dialog the member variables will hold the new control values, and upon error completion the member variables will have the same values that you put into them. This makes calling a modal dialog very simple:

CMyDialog dlg;
dlg.m_Count = somecounter;
dlg.m_Text = sometext;
dlg.m_Option = someBool;
dlg.DoModal( );
somecounter = dlg.m_Count; sometext = dlg.m_Text; someBool = dlg.m_Option;

Nothing to it! But if you ever call UpdateData yourself, this simple paradigm won't work. That's because if the user clicks Cancel, you've already messed over the values to represent some intermediate state that the user has just chosen to reject.

Doug Harrison sent me a critiqe which points out a serious flaw in the above sequence. It is a flaw I never knew about because I have never used a particular feature of dialogs (well, I used it, didn't like it, and chose to deliberately avoid it). But his critique is well-taken, and you should read it. I've included it at the end.

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow