473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Switch statement on string in C++

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[i]->Nodes->Item[j]->Text;
or
(2) std::string s_TC =
string_to_charP(treeview->Nodes->Item[i]->Nodes->Item[j]->Text);

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

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

Thanks.

Nov 17 '05 #1
2 4674
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==]

<77**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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[i]->Nodes->Item[j]->Text;
or
(2) std::string s_TC =
string_to_charP(treeview->Nodes->Item[i]->Nodes->Item[j]->Text);

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

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

Thanks.

Nov 17 '05 #2
"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:OC**************@TK2MSFTNGP14.phx.gbl...
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
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
8
by: Sivas | last post by:
Hi, Can anyone tell me why this does not work: --------------------------------------------- float b = 2.51F; switch(b) {
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
15
by: Benny Raymond | last post by:
I'm confused as to how fallthrough is limited in switch. For example the following works: string switch_test = "a"; switch (switch_test) { case "a": case "b": case "c": doSomething(a);
4
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
5
by: Phuff | last post by:
Hey all, I need some direction help. I have a switch case statement that is seemingly my only option right now, but its too large and not easy to maintain the code. Here goes... I have part...
11
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
3
by: Drowningwater | last post by:
I'm writing a program and I've made a switch statement. I have several quesions: 1. I have a string variable and when I try to use that string variable with the switch function I get a compiling...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.