cancel
Showing results for 
Search instead for 
Did you mean: 

Get Object from a Routine of Different Unit

SOLVED
Josh_147
Contributor

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.

1 ACCEPTED SOLUTION

Accepted Solutions
AlexKaras
Community Hero

Re: Get Object from a Routine of Different Unit

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.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

4 REPLIES 4
BenoitB
Community Hero

Re: Get Object from a Routine of Different Unit

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

)

 

 

Un sourire et ça repart

Josh_147
Contributor

Re: Get Object from a Routine of Different Unit

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
Community Hero

Re: Get Object from a Routine of Different Unit

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.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

Josh_147
Contributor

Re: Get Object from a Routine of Different Unit

Hi,

 

Thanks for the given options. Option (b) will be fine for me as it's my initial version of my script. It seems like not able to share the variable among the different units for my situation.

 

Anyway, thanks a lot for the reply!

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors