HOWTO: DDX - DDV for 64-bit numbers

Last reviewed: July 2, 1997
Article ID: Q169947
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++, 32-bit Editions, versions 4.2b, 5.0

SUMMARY

DDX - DDV functions in MFC do not directly support the __int64 type. However, you can add your own functions to allow 64-bit numbers to be used with Edit controls.

MORE INFORMATION

Below is sample code for will DDX - DDV routines to handle the __int64 type. This code can be easily modified to handle unsigned 64-bit numbers. This code uses the TCHAR datatype for text so it will work with unicode or standard character types without modification.

Sample Code

   // First we have to fix a bug in TCHAR.H.
   // _ttoi64 is incorrectly defined in TCHAR.H, so we need to undefine it
   // and redefine it correctly to avoid the compile error when building
   // and non-unicode version.

   #ifdef _UNICODE
     #ifdef  _ttoi64
       #undef  _ttoi64
       #define _ttoi64 _wtoi64
     #endif
   #else
     #ifdef  _ttoi64
       #undef  _ttoi64
       #define _ttoi64 _atoi64
     #endif
   #endif


   //----------------------------------------------------------------------
   //  Use _ttoi64 and _i64tot to get the data into and out of the
   //  the member variables. These map to _atoi64 /_wtoi64 and
   //  _i64toa / _i64tow.
   //
   void DDX_I64Txt( CDataExchange* pDX,
                    int            nIDC,
                    __int64&       nValue
                  )
   {
     TCHAR        szBuffer[32];
     HWND   hWndCtrl = pDX->PrepareEditCtrl(nIDC);

     if (pDX->m_bSaveAndValidate)
     {                                 // _int64 is 19 characters long
       *szBuffer = NULL;               // when converted to text.
       ::GetWindowText( hWndCtrl, szBuffer, 30 );
       nValue = _ttoi64( szBuffer );
     }
     else
     {
       _tcscpy( szBuffer, _i64tot( nValue, szBuffer, 10 ) );
       ::SetWindowText( hWndCtrl, szBuffer );
     }
   } // end DDX_I64Txt

                 //--------------------------------------------------------
   //  Most DDV functions pass nValue by value. We pass a reference
   //  here so we can set the value to the min or max specified if
   //  an out-of-range value is passed in.
   //
   void DDV_MinMaxI64( CDataExchange* pDX,
                       __int64&       nValue,
                       __int64        nMinVal,
                       __int64        nMaxVal
                     )
   {
     ASSERT( nMinVal <= nMaxVal);
     if( !pDX->m_bSaveAndValidate )
     {
   TRACE0("Warning: initial dialog data is out of range.\n");
   return;
     }

     if( (nValue < nMinVal) || (nValue > nMaxVal) )
     {
       TCHAR  szMinStr[32];          // Have to use strings here because
       TCHAR  szMaxStr[32];          // CString and sprintf formatting
       TCHAR  szMsgStr[64];          // don't support 64-bit numbers.
       _i64tot( nMinVal, szMinStr, 10 );
       _i64tot( nMaxVal, szMaxStr, 10 );
       _stprintf( szMsgStr, "Enter a value between %s and %s\n",
                  szMinStr, szMaxStr );
       AfxMessageBox( szMsgStr, MB_ICONEXCLAMATION );
                                     // Set the value to something defined
       (nValue < nMinVal) ? nValue = nMinVal : nValue = nMaxVal;

       pDX->Fail();                  // Restores focus to the
                                     // offending control and throws
     }                               // a user exception.
   } // End DDV_MinMaxI64.
 

	
	


Keywords : MfcUI
Technology : kbMfc
Version : 4.2b 5.0
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.