Variables and data types in practice

About this tutorial

This tutorial aims to give a beginner friendly approach to topics such as data types, variables, arguments, type mismatch and scoping.
It is not intended as a deep dive but more as a reference to look up when/if problems arise.

What is a variable (in UiPath terms)?

Variable = a “box” that holds data

Every box has:

  1. Name
  2. Data Type
  3. Scope
  4. Value

Variables (and Arguments) can be viewed and configured via the Data Manager.

The 4 most common data types

Data types are as the name suggests different types of data that you can use in your automation.
Each variable has a data type defining how and where it can be used.

Data type Definition When to use it?
String Text If you need to store names, paths, messages etc.
Int32 Whole numbers For example the age of a person
Boolean True/False decisions Determining if a statement or condition is met
Datatable Excel/table-like data If your data comes from Excel → Datatable

Need more/different data types? Click the Browse for Types… option in the data type dropdown.

Arrays vs. Lists vs. Dictionaries

Each of these data types represent Collections of data.
Arrays and Lists are index-based meaning that each item “has its place”/number/index in the Collection (first one starting from 0).
Dictionaries are key/value-pairs where you can lookup/manipulate a value based on its key.

Arrays

  • Fixed-size
  • Static and hard to manipulate
  • Can only hold one data type
{"This","is","a","String","array"}

Lists

  • Dynamic in size
  • Allow for adding/removing items
  • Must be initialized (New List(Of datatype))
  • Can hold different data types
New List(Of Object) From {"Michael",27,True}

There are activities dedicated for easy interaction with Collections such as Lists.

Dictionaries

  • Key/value pairs
  • When you need to lookup by name
  • Data type for key and value can be different
  • Must be initialized (New Dictionary(Of String, String))
New Dictionary(Of String, Int32) From {{"Michael",28},{"Jenny",31}}

Values would be read out like:

dictNamesAndAge("Jenny") -> 31

Variable Scope (the silent killer :sweat_smile:)

Scope defines where the variable is available.

Mnemonics & Best Practice

  • Variables are only usable inside their scope
  • Too small scope → variable not visible/usable
  • Too large scope → messy and unmanagable workflows
  • Declare variables at the smallest scope needed
  • When iterating items do not scope out of loop for item specific variables

Arguments

Arguments are used for passing data between workflows.
You could view them as variables that cross workflow boundaries.
Many beginners will first become familiar with arguments along the way.

Arguments primarily consist of:

  • Name
  • Data Type
  • Direction

The direction defines from where and to data is passed.

Direction Explanation
In Send data into a workflow
Out Get data out
In/Out Both

Type mismatch & common errors

3 classic errors that you might run into.

:warning: Option Strict On disallows implicit conversions from ‘Type1’ to ‘Type2’
:white_check_mark: You are mixing data types. Often .ToString might solve your problem.

:warning: Object reference not set to an instance of an object
:white_check_mark: Your variable is not initialized. See section on Arrays, Lists & Dictionaries above.

:warning: Cannot assign from type ‘Type1’ to type ‘Type2’ in Assign activity ‘Name’
:white_check_mark: The value you are trying to assign does not have the required data type

6 Likes