Forum Discussion

liquidphantom's avatar
liquidphantom
Occasional Contributor
6 years ago
Solved

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.

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    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.

    • liquidphantom's avatar
      liquidphantom
      Occasional Contributor

      Marsha_R wrote:

      Is CC actually = 6 after you set it to column count?

       

      Just curious, why bother to clear the table and recreate?  Tables are free, just create a new one for each test.


      6 is the column count the max column index would be 5.

      I need to use the table over a couple of different scripts. Creating it in the script i'd lose the values when the script ends.

       


      tristaanogrewrote:

      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.


      Thanks, this gave me a nudge in the right direction, this works

      For Col = CC-1 to 0 step -1

      It's weird though that the standard "For x = 0 to n" works fine elsewhere though.

       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        While it may seem weird... it's not.  You are removing columns based upon index.  So, when you remove a column, the index of the remaining columns changes.

         

        FirstColumn has index 0

        SecondColumn has index 1

        ThirdColumn has index 2.

         

        So... you go in and delete FirstColumn.  When that is done...

         

        SecondColumn has index 0

        ThirdColumn has index 1

         

        If I were to go and try and delete ThirdColumn using the index that it had initially (2), I'd get that error that you're experiencing because that index no longer exists... the COLUMN does... but the index for that column has changed.

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    Is CC actually = 6 after you set it to column count?

     

    Just curious, why bother to clear the table and recreate?  Tables are free, just create a new one for each test.