Microsoft KB Archive/111923: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
Line 73: Line 73:


The correct way to test a CString to see whether it is empty (or not empty) is to use the IsEmpty() member function as shown below:
The correct way to test a CString to see whether it is empty (or not empty) is to use the IsEmpty() member function as shown below:
<pre class="codesample">  CString x = &quot;This is a sample CString&quot;;
<pre class="codesample">  CString x = "This is a sample CString";
   if (x.IsEmpty())
   if (x.IsEmpty())
       AfxMessageBox(&quot;The CString is EMPTY&quot;);
       AfxMessageBox("The CString is EMPTY");
   else
   else
       AfxMessageBox(&quot;The CString is not EMPTY&quot;);
       AfxMessageBox("The CString is not EMPTY");
                 </pre>
                 </pre>


Line 88: Line 88:
<br />
<br />
If the CString equality operators (==, !=) are used in an attempt to test whether a CString is NULL, a general protection (GP) fault may occur. The following code fragment demonstrates this:
If the CString equality operators (==, !=) are used in an attempt to test whether a CString is NULL, a general protection (GP) fault may occur. The following code fragment demonstrates this:
<pre class="codesample">  CString x = &quot;This is a sample CString&quot;;
<pre class="codesample">  CString x = "This is a sample CString";
   if (x != NULL)  // GP Fault for (x == NULL) condition, as well
   if (x != NULL)  // GP Fault for (x == NULL) condition, as well
       AfxMessageBox(&quot;The CString is not EMPTY&quot;);
       AfxMessageBox("The CString is not EMPTY");
   else
   else
       AfxMessageBox(&quot;The CString is EMPTY&quot;);
       AfxMessageBox("The CString is EMPTY");
                 </pre>
                 </pre>
Because x is a CString object, not a pointer, it is inappropriate to compare x to a null pointer.<br />
Because x is a CString object, not a pointer, it is inappropriate to compare x to a null pointer.<br />
<br />
<br />
Testing a CString against the empty string, as shown below, is allowed. Because a temporary CString object is constructed, this method is less efficient than calling IsEmpty():
Testing a CString against the empty string, as shown below, is allowed. Because a temporary CString object is constructed, this method is less efficient than calling IsEmpty():
<pre class="codesample">  CString x = &quot;This is a sample CString&quot;;
<pre class="codesample">  CString x = "This is a sample CString";
   if (x != &quot;&quot;)
   if (x != "")
       AfxMessageBox(&quot;The CString is not EMPTY&quot;);
       AfxMessageBox("The CString is not EMPTY");
   else
   else
       AfxMessageBox(&quot;The CString is EMPTY&quot;);
       AfxMessageBox("The CString is EMPTY");
                 </pre>
                 </pre>



Latest revision as of 11:27, 20 July 2020

Knowledge Base


How To Compare a CString to the Empty String

Article ID: 111923

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.51
    • Microsoft Visual C++ 1.52 Professional Edition
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 2.0 Professional Edition
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 5.0 Standard Edition



This article was previously published under Q111923

SUMMARY

The correct way to test a CString to see whether it is empty (or not empty) is to use the IsEmpty() member function as shown below:

   CString x = "This is a sample CString";
   if (x.IsEmpty())
      AfxMessageBox("The CString is EMPTY");
   else
      AfxMessageBox("The CString is not EMPTY");
                

MORE INFORMATION

The CString member function IsEmpty() tests a CString for the empty condition. The function returns nonzero if the string has zero length.

If the CString equality operators (==, !=) are used in an attempt to test whether a CString is NULL, a general protection (GP) fault may occur. The following code fragment demonstrates this:

   CString x = "This is a sample CString";
   if (x != NULL)  // GP Fault for (x == NULL) condition, as well
      AfxMessageBox("The CString is not EMPTY");
   else
      AfxMessageBox("The CString is EMPTY");
                

Because x is a CString object, not a pointer, it is inappropriate to compare x to a null pointer.

Testing a CString against the empty string, as shown below, is allowed. Because a temporary CString object is constructed, this method is less efficient than calling IsEmpty():

   CString x = "This is a sample CString";
   if (x != "")
      AfxMessageBox("The CString is not EMPTY");
   else
      AfxMessageBox("The CString is EMPTY");
                


Additional query words: CString inequality 2.50 2.51 2.52 3.00 3.10 gpf NULL gp-fault

Keywords: kbhowto kbstring KB111923