# Invoke Code

Runs C#, VB, PowerShell or Python code. The code is written in the editor opened with the **Open Editor** button on the activity. Variables and arguments in scope are used in the code by name; values changed by the code are written back to the variables when the code finishes. The running workflow instance is available in every language as `instance`.


---

### Usage

There are two buttons on the canvas:

* **Open Editor**: opens the code editor. C#, VB, PowerShell or Python is selected from the **Change Language** menu. **Save** on the toolbar writes the code and the language to the activity; if the window is closed with unsaved changes, you are asked whether to save. **Open** loads the content of a `.xaml` or `.txt` file into the editor. Errors are shown in the **Error List** at the bottom.
* **Check Code**: compiles C#, VB or Python code; compilation errors are shown in the designer as validation errors. It cannot be used while the code has a syntax error.

The language of a newly added activity is the **Default Code Language** in the settings.

**C# and VB**

* The code is placed in the body of a method; statements are written directly.
* The namespaces imported by the workflow (Imports) are added to the code.
* Variables and arguments are used with the same name and type.
* If the code cannot be compiled, the activity throws an error with the compilation errors.

**PowerShell**

* Variables and arguments are provided as PowerShell variables (`$name`). When the code finishes, those whose value is not empty are written back to the variables.
* The output of the commands is written to the log.
* All PowerShell code runs in the same session; PowerShell variables defined in one run are also visible in later runs.

**Python**

* The code runs in the Python runtime shipped with PAW Robot, in a new scope each time.
* `print` output is written to the log.
* If there is a variable or argument in scope whose name cannot be used in Python (does not start with a letter or an underscore, or is a Python keyword), the activity throws an error.
* `sys.exit()` or `sys.exit(0)` counts as success; any other exit code throws an error. In both cases the variables are written back.
* If a package that is not installed in the project is used, the **Install to Project** button in the **Error List** adds the package to the project.