Unable to remove a column from a Table variable
Absolutly stumped on this one.
I have a ProjectSuite table variable that is 0x0 size when the suite starts. In the first test case to use the table I add each required column, 6 in total, then add the row to add the values for each column. Everything works fine in the first test case.
In the second next case I want to reuse the table but clear everything out and add columns again. I have a Sub that preps the table and does the clear out and it's the same code I've used elsewhere in my suite. However it after removing the first 3 columns it throws the error "The table does not contain a column with the specified index."
I've put a watch on the variable as it steps through the remove loop and the column count value goes red when it hits 3 at which point it throws the error above.
Any one have any ideas?
Code is below (VBscript i'm afraid)
Sub CP_Table_Prep Set TestPermissions = ProjectSuite.Variables.TestPermissions Dim Col CC = TestPermissions.ColumnCount If CC > 0 Then Log.Message("Columns: "&CC) For Col = 0 to CC - 1 Log.Message(TestPermissions.ColumnName(Col)) Call TestPermissions.RemoveColumn(Col) Next Else End If Call TestPermissions.AddColumn("NoContact") Call TestPermissions.AddColumn("Email") Call TestPermissions.AddColumn("Phone") Call TestPermissions.AddColumn("SMS") Call TestPermissions.AddColumn("Post") Call TestPermissions.AddColumn("Online") End Sub
When you remove a column, the column count reduces.... so, when you remove column index 0, now the max number of columns is down to 5.... when you remove 1, it's now 4, when you remove column 2, it's now 3... so, when you remove column 3... well, there are only columns 0, 1, and 2 any more... so column 3 is out of bounds. What I would do, actually, is completely remove the variable and recreate it rather than add/remove columns. There is a ProjectSuite.Variables.RemoveVariable method that will remove the variable... wrap it in logic to check "If variable exists, remove it" and then ProjectSuite.Variables.AddVariable to add it back in.
The alternative option is to run your for loop as a "down to"... stepping down from max to 0.