question

Jeremy R avatar image
0 Likes"
Jeremy R asked Jeremy R commented

Print to variable

In FlexSim, it is possible to produce a string representation of almost any value using the print command. However, there doesn't seem to be an equivalent method for converting values to the string type.

I have a function that accepts a var as input, and the var can contain either a number or a string. Is there a way to convert this value into a string without knowing what its type is?

Trying to simply cast an integer to a string in the following code does not work

var testVal1 = 1;
string result = testVal1.as(string);

Using string.fromNum on a string causes an exception

var testVal2 = "2";
string result = string.fromNum(testVal2);

I realise it is possible to cast the value to a Variant and then switch based on its type, but I was hoping that whatever method is used to convert data of any type to a string in the print function is available for users too.

FlexSim 18.2.3
flexsim 18.2.3stringcastprint
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Phil BoBo avatar image
1 Like"
Phil BoBo answered Jeremy R commented

Use string concatenation (if using FlexSim 19.2 or later):

var one = "1";
var two = 2;
string twelve = "" + one + two;
return twelve; // returns "12"
var testVal1 = 1;
string result = "" + testVal1;
var testVal2 = "2";
string result = "" + testVal2;

Also, "var" is an inferred type, like the "auto" keyword in C++. The compiler knows what type it is, you just don't have to spell it out.

If you "have a function that accepts a var as input, and the var can contain either a number or a string," then I assume you are getting that "var" using the param() function.

param() returns a Variant. The variables in your example have types: string and int. In your function, your variable's type is probably a Variant, which can hold strings, numbers, arrays, nodes, or nullvar. You aren't "casting" from a "var" to a "Variant." Your variable already is a Variant, you just didn't spell it out in your function, instead choosing to use the var keyword to infer its type.

As you realized, you can use the type member on the Variant class to determine what its value is. This is ultimately what the print() command does to determine how to print a Variant's value as a string. For example:

Variant testVal1 = param(1);
string result = "";
if (testVal1.type == VAR_TYPE_NUMBER) {
    result = string.fromNum(testVal1);
} else if (testVal1.type == VAR_TYPE_STRING) {
    result = testVal1;
}
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jeremy R avatar image Jeremy R commented ·

Ah, I think wasn't sufficiently understanding the distinction between var and Variant. Thanks for the explanation.

I'm using 18.2, so the string concatenation method won't work for me right now, but I'll keep it in mind if I use later versions. For now, it looks like switching on Variant.type is what I'm going to need to do.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.