Get a Table row / check if it exists without exceptions

Get a Table row / check if it exists without exceptions

sastanin
Collaborator Collaborator
117 Views
4 Replies
Message 1 of 5

Get a Table row / check if it exists without exceptions

sastanin
Collaborator
Collaborator

[ FlexSim 17.0.6 ]

Currently (FlexSim 17.0 & 17.1), Table class has a index operator which allows to get rows by name, but it fails with an uncatchable exception if a row with such header does not exist.

This syntax is nice:

Table tbl = somenode;
return tbl["somerow"][column_name_or_number];  // OK

But there is a problem. Table class doesn't provide any method to check if there is a row with a given name in the table, and there is no way to handle exceptions in FlexScript.

This code throws an exception:

Table tbl = somenode;
return tbl["nonexistent_row"][column_name_or_number];

The only workaround to avoid exceptions is to explicitly iterate over all Table rows, and do not use the index access at all. In the long term, an exception handling mechanism in FlexSim would help.

Could you please return a nullvar if the row (or column) doesn't exist? And/or add a way to check if there is a row or column with a given name?

Accepted solutions (1)
118 Views
4 Replies
Replies (4)
Message 2 of 5

Jacob_Gillespie
Autodesk
Autodesk

I think you are right that the class doesn't give you any way to do that.

However you could do something like this:

Table table = Table("GlobalTable1");
string row = "Row 1";
string col = "Col 1";
if(table.as(treenode).find(row + "/" + col))
	return table[row][col];
Message 3 of 5

philboboADSK
Autodesk
Autodesk
Accepted solution

I will add a note to the dev list to improve this. You are correct that it should either return a nullvar or we should add a way to check if the cell exists. We will probably update it so that it returns a nullvar to match how getting labels and subnodes by name works.

Thanks Serge.



Phil BoBo
Sr. Manager, Software Development
Message 4 of 5

sastanin
Collaborator
Collaborator

Thank you, right now I just write loops like

for (int i = 1; i <= tbl.numRows; i++) {
	if (tbl.getRowHeaders() == "whatever") {
		// ...
	}
}

but the new syntax is certainly nicer.

0 Likes
Message 5 of 5

sastanin
Collaborator
Collaborator

Thank you, Phil. Returning nullvar sounds like a fine solution.

0 Likes