Switch statement on string in C++

  • Thread starter Thread starter 7777777.chen
  • Start date Start date
7

7777777.chen

Is it true that VC++ doesn't support switch on string data type?
Did anyone know how to handle the following situation?

(1) System::String* s_TC =
treeview->Nodes->Item->Nodes->Item[j]->Text;
or
(2) std::string s_TC =
string_to_charP(treeview->Nodes->Item->Nodes->Item[j]->Text);

Switch (s_TC) {
case "TC_1":
break;
case "TC_N":
break;
..
..
}

Both (1) and (2) all have compile error.

Thanks.
 
Switch statements are really just a 'convenience'. Although not as pretty to
look at, do this:

if ( s_TC == "TC_1" ) {}
else if ( s_TC == "TC_N" ) {}

[==P==]
 
Peter Oliphant said:
Switch statements are really just a 'convenience'.

That's not entirely true. Switch statement do offer optimization
possibilities over chained if()s. (Well, let's say "switch" make it easier
to recognize possible optimazations). However these are pretty much only
available when the object being switch on is an int. Which is why in C/C++
switch only works with int-like data types. The "prettiness" factor was
only realized later, at which point some languages started to shoe-horn
switch statements on other data types into their syntax.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Back
Top