Forum Discussion

Josh_147's avatar
Josh_147
Contributor
4 years ago
Solved

Get Object from a Routine of Different Unit

Hi all!

I'm trying to pass the project variables that I set in Unit1 Loop_A into Unit2 Loop_B but I always get an error.

 

 

'[Unit1]
Sub Loop_A
    Set Var = Project.Variables.varA
End Sub

'[Unit2]
'USEUNIT Unit1
Sub Loop_B
    '(How can I get the object "Var" that I set in Unit1 Loop_A?)
End Sub

 

 

From the documentation, I only can get how to call/run the routine from different unit. Can I just get the object/value instead of call/run the routine?

 

Thanks.

  • Hi,

     

    Three options:

    a) Declare Var variable in Unit1 as public/global as suggested by BenoitB. Global variable can be set/get from any function (if its unit is referenced as Benoit demonstrated). Global variables from code units are not the best recommended approach (with possible side effects), so options b) and c)

    b) Create one more Project Variable and use it instead of script variable Var;

    c) Make Loop_A and Loop_A1 to be not subroutines but functions that return the object referenced by the Var variable.

     

4 Replies

  • BenoitB's avatar
    BenoitB
    Community Hero

    If you set a variable inside a function, the scope of it is the function only.

    If you want to use variable across unit then you must define it as a global variable and it would be accessed whenever you add the useunit containing the variable declaration.

     

    Pseudo-code :

    unit A

    var X

     

    unit B

    function Alpha(

       x = x +5    // Don't works

    )

     

    unit C

    useunit A

    function Beta(

      x = x + 5     // Works

    )

     

     

    • Josh_147's avatar
      Josh_147
      Contributor

      Hi BenoitB ,

       

      The reason I set a variable in a function is due to there are other functions in the same unit.

       

      '[Unit1]
      Sub Loop_A
          Set Var = Project.Variables.varA
      End Sub
      
      Sub Loop_A1
          Set Var = Project.Variables.varA1
      End Sub
      
      '[Unit2]
      'USEUNIT Unit1
      Sub Loop_B
          '(How can I get the object "Var" that I set in Unit1 Loop_A?)
          '(How can I get the object "Var" that I set in Unit1 Loop_A1?)
      End Sub

       

      The variable passed to Loop_B in Unit2 will be depends on which function (Loop_A or Loop_A1) is running. Is it possible to do this? Or I have to separate Loop_A in Unit1 and Loop_A1 into another Unit?

       

      Thanks.

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        Three options:

        a) Declare Var variable in Unit1 as public/global as suggested by BenoitB. Global variable can be set/get from any function (if its unit is referenced as Benoit demonstrated). Global variables from code units are not the best recommended approach (with possible side effects), so options b) and c)

        b) Create one more Project Variable and use it instead of script variable Var;

        c) Make Loop_A and Loop_A1 to be not subroutines but functions that return the object referenced by the Var variable.