How to use a variable of a code module to another code modul

Ask general questions here.
ananth.shenoy
Posts: 1
Joined: Thu Aug 25, 2016 10:16 am

How to use a variable of a code module to another code modul

Post by ananth.shenoy » Thu Aug 25, 2016 10:22 am

I have Created a variable in one code module.Can i use that variable in another code module?If SO how to use

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to use a variable of a code module to another code modul

Post by krstcs » Thu Aug 25, 2016 3:51 pm

Please read the user guide, it explains clearly how to handle this situation.

http://www.ranorex.com/support/user-guide-20.html

You probably will need to use a Global or Test Case parameter to hold the value of the variable between module executions. Bind the parameter to the variables in each module.
Shortcuts usually aren't...

User avatar
N612
Posts: 135
Joined: Mon Jul 11, 2016 4:01 pm

Re: How to use a variable of a code module to another code modul

Post by N612 » Thu Aug 25, 2016 3:57 pm

First, Declare the variable and make it static: (Right Click in code module > Insert New Module Variable)

Code: Select all

static string _myVar = "testVar";
[TestVariable("0f6808ec-bef9-44de-ae74-2a7de9eb6baf")]
static public string myVar
{
    get { return _myVar; }
    set { _myVar = value; }
}
Now you can use this variable in another code module. There are two ways you can use it:

1. With Inheritance:

Code: Select all

public class CodeMod2 : CodeMod1, ITestModule
myVar = "myVar-Modified";
Note: This will also work without making your variable static but will be its own instance when inherited.

2. Without Inheritance:

Code: Select all

CodeMod1.myVar = "myVar-Modified";