Forum Discussion

neethy_ashok's avatar
neethy_ashok
Occasional Contributor
13 years ago

Declaring Array as Project Variables

Please let me know  how to declare the "MyArray" in the below code as project variable which will be used in different scripts.



CST="1,2,3,4,5,"

MyArray= Split(CST1,",")

7 Replies

  • maximojo's avatar
    maximojo
    Frequent Contributor
    if I understand you the jscript should be:



      if(!Project.Variables.VariableExists("MyArray"))

      {

        Project.Variables.AddVariable("MyArray", "Object");

      }



    Project.Variables.MyArray = CST.split(",");




  • Firstly, what I think you're trying to accomplish is probably better done as a script entirely. I haven't used VB in a long time so i'm not going to try and code this. But nonetheless, from what I can tell your script appears to declare one array in script (CST), then create a object variable in the project ("MyArray") and then tries to save CST into MyArray with line breaks (if my memory of VB serves me correctly).



    For a Project Variable to hold an array (2 dimensional only) it would have to be a 'Table' type and not an 'Object' type. In dealing with a 'Table' type variable, you need to define it as follows (this is from the help menu):



    Sub TableVarTest

      Dim t

      ' Create a table variable if it doesn't exist

      If Not Project.Variables.VariableExists("MyTable") Then

        Call Project.Variables.AddVariable("MyTable", "Table")

      End If

     

      ' Get a reference to the variable

      Set t = Project.Variables.VariableByName("MyTable")

     

      ' Add columns to the table

      Call t.AddColumn("Name")

      Call t.AddColumn("Age")

      Call t.AddColumn("City")

     

      ' Create two rows

      t.RowCount = 2



      ' Fill in the table

      ' The first row

      t.Name(0) = "John Smith"

      t.Age(0) = 24

      t.City(0) = "Seattle"

     

      ' The second row (using the Item property)

      t.Item("Name", 1) = "Bob Feather"

      t.Item("Age", 1) = 59

      t.Item("City", 1) = "Milltown"

    End Sub



    ------



    To me it seems that it would be easier to define the array entirely in VB.


  • neethy_ashok's avatar
    neethy_ashok
    Occasional Contributor
    I Have used Table object and it is working fine.Thank you very much
  • neethy_ashok's avatar
    neethy_ashok
    Occasional Contributor
    Please give the code for VBscript



    I tried  with the below code.But Type mismatch error is occuring.





    sub Proj_Array

    dim CST

    CST="1,2,3,4,5,"

      if Project.Variables.VariableExists("MyArray") then

      else

        Project.Variables.AddVariable "MyArray", "Object"

        end if

    Project.Variables.MyArray =split(CST,",")

    for i=0 to UBOUND (Project.Variables.MyArray-1)

       log.Message Project.Variables.MyArray(0)

    next

    end sub



  • maximojo's avatar
    maximojo
    Frequent Contributor
    Hi



    I'm not familiar with VB. Where is the type mismatch in your code?
  • neethy_ashok's avatar
    neethy_ashok
    Occasional Contributor
    Type mismatch occurs at the below line



    Project.Variables.MyArray =split(CST,",")

  • neethy_ashok's avatar
    neethy_ashok
    Occasional Contributor
    Declaring it as Table might  work...I would prefer to make it simple.

    But I want to declare a one dimensional array as a project varianble.I do not want to declare Multi dimensional Array.I have this 1,2,3,4,5 numbers i want to store this to a single dimension array in which the data i can access like below

    Myarray[0]

    MyArray[1]

    MyArray[2]

    MyArray[3]

    MyArray[4]