ATTENTION

This 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

Go Back   FlexSim Community Forum > FlexSim Software > Q&A
Downloads

Q&A Using Flexsim and building models

  #1  
Old 05-24-2010
Congshi Wang Congshi Wang is offline
Student
 
Join Date: May 2010
Location: Germany
Posts: 58
Downloads: 2
Uploads: 0
Thanks: 48
Thanked 0 Times in 0 Posts
Rep Power: 124
Congshi Wang is on a distinguished road
Default 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  
Old 05-24-2010
RalfGruber's Avatar
RalfGruber RalfGruber is offline
FlexSim Software Products
 
Join Date: Jul 2007
Location: Orem, UT, USA
Posts: 195
Downloads: 37
Uploads: 0
Thanks: 518
Thanked 294 Times in 124 Posts
Rep Power: 345
RalfGruber is a splendid one to beholdRalfGruber is a splendid one to beholdRalfGruber is a splendid one to beholdRalfGruber is a splendid one to beholdRalfGruber is a splendid one to beholdRalfGruber is a splendid one to beholdRalfGruber is a splendid one to behold
Default

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  
Old 05-24-2010
Lars-Olof Leven Lars-Olof Leven is offline
Flexsim Distributor
 
Join Date: Aug 2007
Location: Sweden, Borlnge
Posts: 312
Downloads: 278
Uploads: 2
Thanks: 300
Thanked 256 Times in 139 Posts
Rep Power: 330
Lars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to behold
Default

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  
Old 05-26-2010
Brandon Peterson's Avatar
Brandon Peterson Brandon Peterson is offline
The Flexsim Consultant
 
Join Date: Jul 2007
Location: Salt Lake City, Utah
Posts: 382
Downloads: 29
Uploads: 6
Thanks: 192
Thanked 516 Times in 235 Posts
Rep Power: 490
Brandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant futureBrandon Peterson has a brilliant future
Default

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  
Old 05-26-2010
Lars-Olof Leven Lars-Olof Leven is offline
Flexsim Distributor
 
Join Date: Aug 2007
Location: Sweden, Borlnge
Posts: 312
Downloads: 278
Uploads: 2
Thanks: 300
Thanked 256 Times in 139 Posts
Rep Power: 330
Lars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to beholdLars-Olof Leven is a splendid one to behold
Default

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


All times are GMT -6.
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Copyright 1993-2018 FlexSim Software Products, Inc.