Variables and scope

Before discussing variables, let us take a look at Memory and its structure:

Memory consists of millions of memory Cells and each memory cell stores data in the form of 0s and 1s (binary digits). Each cell has a unique address, and by using this address, the cell can be accessed:

When data is stored in memory, its content gets split into further smaller forms (binary digits). As shown in the preceding diagram, 2 bytes of data consists of several memory cells.

A variable is the name that is given to a particular chunk of memory cells or simply a block of memory and is used to hold data.

You can declare any desired name and create a variable to store the data.

It is recommended, however, that we use meaningful variable names. For example, if we wish to create a variable to store the name of a person, then we should declare

Name: Andy

It is a good practice to create meaningful variable names. This becomes very useful in debugging the program.

As we discussed, a variable is used to store data. Data is present around us in different types—it can be an mp3 file, text file, string, numbers, and so on. That is why variables are associated with their respective data types. A particular type of variable can hold only that type of data. If there is a mismatch between the data and the variable type, then an error occurs. The following table shows the type a of variable available with UiPath:

Type Content
Integer Whole numbers
String Text of any kind: "The Quick Fox @4598" 
Boolean True or false
Generic Anything

 In UiPath, we can declare a variable in the Variables section. Just give it a meaningful name and select the appropriate type from the drop-down list.

By a meaningful variable, it is implied that the variable name should not be ambiguous. Try to make it as descriptive as possible so that the person reading the code understands the purpose of the variable. Good examples are daysDateRange, flightNumber, and carColour. Bad examples are days, dRange, temp, and data.

We can also specify the scope of a variable. The Scope is the region under which the data has its effect or availability. You can choose the Scope of the variable according to your requirements; try to limit it as far as possible. Please refer to the following screenshot to understand Variables panel:

Creating a variable 
For security reasons, it is not a good practice to set the Scope of your variable to fullest as it may accidentally be accessed by another region or could be modified.

Let us take an example of creating a variable and then displaying a Message box using that variable:

  1. We have declared a variable as name in the Variables section and set its Default value to "Hello world". By default, the type of the variable is String (we can change its type according to our needs). 
  2. Search for Message box in the Activities panel. Drag and drop that Message box template into a Flowchart.
  3. Right-click on the message template and select Set as Start node:
  1. Double-click on the Message box template and specify the variable name that we created earlier. At this stage, we are ready to run our application by simply clicking on the Run button:

A dialogue box will pop up with the "Hello world" text displayed on it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.236