Using Strings with a Switch | CodeGuru

Using Strings with a Switch

Environment: The following macros help to achieve the switch-case on a string. ////////////////////////////////////////////////////////////// #define STR_SWITCH(str) { TCHAR* __ps = (TCHAR*)((const TCHAR*)str); while(1) { #define STR_SWITCH_END() break; } } #define STR_CASE(str) if(0 == _tcsicmp(__ps,((const TCHAR*)str))) #define STR_CASE_EXACT(str) if( 0 == _tcscmp( __ps, ((const TCHAR*)str) ) ) #define DEFAULT_CASE() ////////////////////////////////////////////////////////////// How do you use these macros? Go […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 8, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment:

The following macros help to achieve the switch-case on a string.

//////////////////////////////////////////////////////////////
#define STR_SWITCH(str)  {
        TCHAR* __ps = (TCHAR*)((const TCHAR*)str);
        while(1) {

#define STR_SWITCH_END()  break; } }

#define STR_CASE(str) if(0 == _tcsicmp(__ps,((const TCHAR*)str)))

#define STR_CASE_EXACT(str)  if( 0 == _tcscmp( __ps,
                               ((const TCHAR*)str) ) )

#define DEFAULT_CASE()

//////////////////////////////////////////////////////////////

How do you use these macros? Go through the following piece of code.

Myclass::Function()
{
  CString sSwitch("bbbb");
  CString sCase("AAAA");

  STR_SWITCH(sSwitch)      //Start of switch
  {                        //Opening and closing braces
                           //NOT MANDATORY for switch.
  STR_CASE(sCase)
    {                      //Opening and closing braces
                           //MANDATORY for case.
      AfxMessageBox("In first case statement");
      break;               //break has to in braces of case
    }                      //Opening and closing braces
                           //MANDATORY for case.

  break;                   //Illegal break -- The code works,
                           //but the output....?????

  sCase = _T("bbbb");      //Statements allowed here ;)

  STR_CASE_EXACT(sCase)
    {
      AfxMessageBox("In second case statement");
      break;
    }
  DEFAULT_CASE()
    {
                           //Default handling if any
      break;
    }
  }                        //Opening and closing braces
                           //NOT MANDATORY for switch
  STR_SWITCH_END()         //MANDATORY statement
}

STR_CASE() -> Perform a lowercase comparison of strings.
STR_CASE_EXACT() -> Compares strings, matches the
                     case of the characters.
DEFAULT_CASE() -> Default case for the switch.

Note the following requirements:

  1. The “break” statement has to be inside the scope for the STR_CASE, STR_CASE_EXACT, or DEFAULT_CASE.
  2. Fall through for case statements is not allowed with this switch case.
  3. Statements between two cases are allowed. Be careful while modifying the string on which the switch is performed.
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.