question

Gui Vaccaro avatar image
0 Likes"
Gui Vaccaro asked Gui Vaccaro commented

Ambiguity in length of string label

Hello,
Imagine I have a string label "test" created in a 3D object.
If this label is assigned with an empty string (""), I noticed the following:

myObject.test.length returns 1
myObject.test.as(string).length returns 0

Why does that happen?

FlexSim 25.0.1
stringlength
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

Felix Möhlmann avatar image
2 Likes"
Felix Möhlmann answered Gui Vaccaro commented

In the first case the value gets treated as a Variant for which the "length" property returns the number of elements if the value is an array, 0 if it's a null value and 1 otherwise.

The "length" property of a string is the byte length of the string, so 0 for the empty string.

· 3
5 |100000

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

Gui Vaccaro avatar image Gui Vaccaro commented ·

Hello @Felix Möhlmann ,

thank you for your explanation. However, should not defining a label as "string data" suffice to cast it as "not a variant"?

The inference from your response is that all "length" calls for string labels need to be manually casted to avoid code misbehavior.

The misbehavior still happens in the code below

string test = myObject.test;
print(test.length); // returns 1

The only "fixes" are these

string test = myObject.test.as(string);
print(test.length); // returns 0

or

print(myObject.test.as(string).length); //returns 0
0 Likes 0 ·
Felix Möhlmann avatar image Felix Möhlmann Gui Vaccaro commented ·

I would think the compiler assumes a Variant because while the label might be set as containing string data this could change thoughout the model run.

The first example code you posted should work though (casting the label to a string variable and then printing its length).

1736838304388.png

Could you maybe upload the entire example?

0 Likes 0 ·
1736838304388.png (69.4 KiB)
Gui Vaccaro avatar image Gui Vaccaro Felix Möhlmann commented ·

Hi, @Felix Möhlmann ,

thank you. There is not much of an example. The only missing aspect from the case that triggered this question is that the original value was not an "empty string token" (i.e. ""), but an attribution from an empty cell from a global table.

Something like:

myObject.test = myGlobalTable[1]["value"] //column 'value' is previously 
                                            assigned as string/text

string test = myObject.test;

print(test.length); // returns 1

Anyway, my takeaway from all this is that every label needs to be explicitly casted as string if string attributes/functions are expected to work properly.

Thank you.

0 Likes 0 ·