Ambiguity in length of string label

Ambiguity in length of string label

guiroehe
Collaborator Collaborator
216 Views
4 Replies
Message 1 of 5

Ambiguity in length of string label

guiroehe
Collaborator
Collaborator

[ FlexSim 25.0.1 ]

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?

0 Likes
Accepted solutions (1)
217 Views
4 Replies
Replies (4)
Message 2 of 5

moehlmann_fe
Advocate
Advocate
Accepted solution

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.

Message 3 of 5

guiroehe
Collaborator
Collaborator

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
Message 4 of 5

moehlmann_fe
Advocate
Advocate

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
Message 5 of 5

guiroehe
Collaborator
Collaborator

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