From the course: Complete Guide to UiPath RPA Development

Variable scope

- [Speaker] Alright, so variable scope is a very important topic. Let's go check it out. In any kind of programming or RPA, there are two important concepts. Memory management and reducing confusion. Our computers use something called RAM or random access memory to hold data that computer programs or RPA processes are operating on. Ideally, any data your program or RPA process is working with should only be held in RAM for as long as it's actually needed. Once you're done with it, that memory should be freed up for something else. Otherwise, lots of applications that have lots of irrelevant data floating around in RAM would eventually fill your RAM up and reduce performance of your computer. From a confusion reduction standpoint, programs and RPA processes can get very big sometimes. And when you're down here in the variables panel, if you saw 1000 variables in here, that can be a bit overwhelming. So ideally, we only want to see variables that are relevant for a specific part of the automation we might be working on. You wouldn't want to see all the variables for the whole project every time you open this. So that's where variable scope comes in. Right now, I've got this container called explore variables and all of our variables for this container are visible here in this variables panel. Interestingly, if I was to go into activities and grab a new sequence and drag and drop it at the bottom here or at the top, wherever, and then I grab and assign and drag it in here and right click to create a new variable called company name for example, and open quotes to give it a name. I'm going to click on that sequence and rename it to sub-routine. And, of course, this is just a generic term to represent that whatever's happening inside this block is a small piece of work relative to the larger process. So when I'm down here on subroutine, we see company name and all of the variables that are in a wider or larger scope above this subroutine. So what that tells you is any activities inside this subroutine are going to be able to take advantage of any variables that were declared in a parent container that contains this. However, the reverse is not true. If I scroll back up and click on this parent container, now we see that the company name variable is no longer there and that's because this parent scope doesn't or shouldn't care about the data values that are being worked on in this child scope. One challenge that can occur and trip people up is if I was at the top of this sequence and I created a new sequence in here, and we'll call it account setup, for example, if I was to grab another read text activity and drag that in here because maybe we're reading a customer file and I go over here to the output and right click and create a variable here called customer data, now we see customer data because I've clicked on this activity, which is inside this child sequence within my greater flow. The challenge is if I now try to use that variable that was created inside this sub-routine or child container, when I try to log message for example and click here open quotes and say, "The customer data is at a plus", now we notice that customer data is not in this list. And if I was to try to click on this and Control C to copy that and paste it here, when I click away, I'm getting a complaint that says customer data is not declared. It may be inaccessible due to its protection level. And really what that means is this sequence is protecting any variables that were declared inside of it from being seen in a larger scope. And notice when I'm out here in this wider scope, again, we don't see that customer data variable here. So if you get yourself into this situation, the way to fix it is to simply click on the sequence or container that holds that variable you're looking for. Find that variable and you can actually click in the scope column here and you can change the scope. So I can raise it out of account setup and instead, place it into explore variables. So now I have promoted the scope of that variable out of this sub-container and into this parent container. So now that error goes away because now my customer data variable has a scope of explorer variables. And you can go the other direction as well, but it's a little more complicated. If I decide that customer data only makes sense to this child sequence, I can't just click on this and move it in there. What I have to do first is click on that child sequence and notice in this child sequence, we can still see variables from the greater scope. Now though, I can click on this and I can move it into account setup, which of course, causes this error to return because now that customer data variable is in a smaller scope and not visible out here in this parent container. And the last thing I'll say about this quick is scope allows you to use the same variable name in different scope contexts. And what I mean by that is if I was on this explore variables scope and I tried to create another variable called last name for example, it's going to complain at me and say, there's already a variable name, last name in this scope, please choose a different name. But what I can do is if I create another sequence under this one, for example, we know that I've got customer data in this scope. I could also, if I wanted to, I'll drag in a sign in here, I'll right click and create a variable called customer data inside here. And when I click away, notice I'm not getting any variable complaints. Obviously, I'm still getting a complaint here because I lost my file name. I'll just put some quotes in there that cleans that one up. And this one is still complaining because it can't see customer data. But if I was to move this into this scope, now it's suddenly happy again. And what's funny is I could move this into this scope as well and it would be equally happy because this customer data variable, even though it might have different values exists in these two different scopes. And once my automation passes through here, it's going to dispose of this customer data value. And now when it moves into this scope, this customer data value will be stored in a different part of RAM and have a different value. And usually, reusing variables in different scopes isn't something like customer data, it's going to be something more like index or loop value or something innocuous like that. Hope all that makes sense.

Contents