ATTENTIONThis FlexSim Community Forum is read-only. Please post any new questions, ideas, or discussions to our new community (we call it Answers) at https://answers.flexsim.com/. Our new Question & Answer site brings a modern, mobile-friendly interface and more focus on getting answers quickly. There are a few differences between how our new Q&A community works vs. a classic, threaded-conversation-style forum like the one below, so be sure to read our Answers Best Practices. |
flexsim.com |
#1
|
|||
|
|||
switch function with string
Can I use the switch function with strings too?
For example: int vehicletype = gettablestr("vehicle",1,2); switch (vehicletype) { case "car": { coloryellow(item); break; } case "truck": { colorred(item); break; } case "plane": { colorgreen(item); break; } } Thanks a lot! |
#2
|
||||
|
||||
Congshi,
no you can´t use a string to switch. Please take a look into the Flexsim User Manual (under help within Flexsim). In the section "Writing Logic in Flexsim" you can find information about coding in Flexsim. Hope that helps! Good luck Ralf Flexsim Germany |
The Following User Says Thank You to RalfGruber For This Useful Post: | ||
Congshi Wang (07-26-2010) |
#3
|
|||
|
|||
Hi,
No you can only use numbers. Because you can easily change to C/C++ instead of Flexscript and therefore Flexscript must be compatibly with C/C++. In C/C++ switch can only take numbers. Ralf was quicker then me. Lars-Olof Last edited by Lars-Olof Leven; 05-24-2010 at 11:55 AM. Reason: Faster respond. |
The Following User Says Thank You to Lars-Olof Leven For This Useful Post: | ||
Congshi Wang (05-24-2010) |
#4
|
||||
|
||||
Guys,
Here is something interesting to try as an alternative to using numbers. First, I entered the following in the Global Macros tab in Global Variables: #define VT_CAR 1 #define VT_TRUCK 2 #define VT_BUS 3 Second, I created a table (name = "Test") with a column of strings and randomly entered in one of the #defines from above. Third, I placed the following code in a script window and executed it: for(int row = 1; row < gettablerows("Test"); row++) { switch(executefsnode(gettablecell("Test", row, 1), NULL, NULL, NULL, 0)) { case VT_CAR: msg("TYPE", "CAR"); break; //Could have put 1 instead of VT_CAR case VT_TRUCK: msg("TYPE", "TRUCK"); break; //Could have put 2 instead of VT_TRUCK case VT_BUS: msg("TYPE", "BUS"); break; //Could have put 3 instead of VT_BUS } } This will be slower than using numbers because you are calling executefsnode but you don't have to worry about having a cryptic number instead of a sting in your table either. *I could have used "Car", "car", "CAR", etc. instead of "VT_CAR" for my #define name but I placed the VT ("Vehicle Type") in there so that it would be more unique and less likely to interfere with a variable somewhere. **Also, I believe that you can use either integers or characters in C++ switch statements while Flexscript limits you to integers. Good Luck, Brandon
__________________
thats not normal. |
The Following 2 Users Say Thank You to Brandon Peterson For This Useful Post: | ||
RalfGruber (05-26-2010) |
#5
|
|||
|
|||
Hi,
It is correct that in C/C++ you can either use integer or char, but that is because char is treated as number in C/C++ 0 to 255. Lars-Olof |
The Following User Says Thank You to Lars-Olof Leven For This Useful Post: | ||
Congshi Wang (07-26-2010) |
Thread | Thread Starter | Forum | Replies | Last Post |
Global Table string value | Congshi Wang | Q&A | 1 | 05-24-2010 07:38 AM |
eventget(1,4) - how to get the string? | Tom David | Q&A | 1 | 11-20-2009 07:56 AM |
4.5 bug with switch statement | Anthony Johnson | Product Announcements | 0 | 02-10-2009 06:38 PM |
String as label of a flowitem | Matthias Hofmann | Q&A | 2 | 09-18-2008 11:15 AM |
IF with a String | Gavin Douglas | Q&A | 1 | 07-14-2008 12:36 PM |