UiPath Pitfall #4: A Variable With the Same Name as Your Argument
UiPath Pitfall #4: A Variable With the Same Name as Your Argument
You assign a value to your Out argument, a Message Box right after says it’s filled, and a few activities later (or back in the parent workflow) it’s Nothing. No error and no runtime warning. It’s just empty. This came up again on the UiPath Forum in Out param Nothing when leaving Do, and it’s one of those bugs that makes you doubt Studio itself.
你为 Out 参数赋值后,紧接着的 Message Box 显示它已填充,但几个活动之后(或回到父工作流时),它却变成了 Nothing。没有错误,也没有运行时警告,它就是空的。这个问题最近又出现在 UiPath 论坛的“Out param Nothing when leaving Do”讨论中,它是那种会让你怀疑 Studio 本身的 Bug 之一。
The cause: Most of the time, a variable and an argument share a name. You’ve got out_Result in the Arguments panel, and somewhere along the way a variable called out_Result also got created, often scoped to a Sequence or a Do block. Inside that scope, your Assign writes to the variable. Outside it, you’re reading the argument, which never got touched. The Managing Arguments docs spell it out: if a variable and an argument have the same name, the variable is always the one used at runtime. The same goes for two variables in different scopes of one file. The inner scope wins.
原因:大多数情况下,变量和参数重名了。你在 Arguments 面板中有名为 out_Result 的参数,而在流程的某个地方,又创建了一个也叫 out_Result 的变量,通常其作用域限定在某个 Sequence 或 Do 块中。在该作用域内,你的 Assign 活动写入的是变量;而在作用域外,你读取的是参数,而参数从未被赋值。Managing Arguments 文档明确指出:如果变量和参数同名,运行时总是优先使用变量。对于同一文件中不同作用域的两个变量也是如此,内部作用域优先。
How it sneaks in: For me it’s usually Ctrl + K in the To field of an Assign. That shortcut creates a new variable, not an argument. So if you type the argument’s name and hit Ctrl + K, you’ve just made a duplicate scoped to wherever you were. (Ctrl + M is the one that creates an In argument.)
它是如何混入的:对我来说,通常是在 Assign 活动的 To 字段中使用 Ctrl + K 时发生的。该快捷键创建的是新变量,而不是参数。因此,如果你输入了参数名称并按下 Ctrl + K,你就创建了一个作用域限定在当前位置的重复项。(Ctrl + M 才是创建 In 参数的快捷键。)
The fix: Find the duplicate. Select the container (click the Do, Sequence or Try block, not just the root) and check the Variables panel for the argument’s name. Delete the variable, keep the argument, then re-pick the argument in the Assign.
解决方法:找到重复项。选择容器(点击 Do、Sequence 或 Try 块,不要只选根节点),并在 Variables 面板中检查是否存在该参数名称。删除变量,保留参数,然后在 Assign 活动中重新选择该参数。
Let Workflow Analyzer catch it. Run Analyze File or Analyze Project from the Design ribbon. There are rules for exactly this: ST-NMG-006 Variable Overrides Argument, ST-NMG-005 Variable Overrides Variable. If you’re on a team, set ST-NMG-006 to Error in the Workflow Analyzer settings. Then it shows up as an error in the Errors panel, which is a lot harder to scroll past.
让 Workflow Analyzer 来捕捉它。从 Design 功能区运行 Analyze File 或 Analyze Project。专门针对此问题有相关规则:ST-NMG-006 变量覆盖参数,ST-NMG-005 变量覆盖变量。如果你在团队中工作,请在 Workflow Analyzer 设置中将 ST-NMG-006 设置为 Error。这样它就会在 Errors 面板中显示为错误,让你很难忽略它。
Use direction prefixes on arguments only. in_, out_, io_ for arguments, never for variables. A variable called out_Something then looks wrong the moment you see it. Assign the Out argument once. If the value gets built up across several scopes, work on a local variable and assign it to the Out argument once, at the end of the workflow: ’ inside the scopes, work on a local variable resultBuilder = resultBuilder & currentRow(“Name”).ToString ’ last activity in the workflow out_Result = resultBuilder
仅在参数上使用方向前缀。in_、out_、io_ 仅用于参数,绝不用于变量。这样,当你看到名为 out_Something 的变量时,一眼就能看出不对劲。Out 参数应只赋值一次。如果数值是在多个作用域中累积生成的,请使用局部变量进行处理,并在工作流结束时将其赋值给 Out 参数一次: ’ 在作用域内,使用局部变量 resultBuilder = resultBuilder & currentRow(“Name”).ToString ’ 工作流中的最后一个活动 out_Result = resultBuilder