Use a variable which it's name is in a variable

Hi All,

I´ve a question, I´m using a datatable to get different values and one of the values that i get is the name of the variable that i need to use that is defined in the workflow and has a different value.

So, my question is, ¿How can i refer to a variable name using the value of a different value?

Thanks in advance.

Hi,

Sounds like you want the equivalent of Evaluate() or something, which I don’t believe is available in vb.net that UiPath uses.

You can however work around this by replacing the word that represents the variable with the variable in the code.

So if your string value is like this for example: “My name is username”
You can replace “username” with your variable like:

value.Replace("username",username)

In my opinion, it is also better to surround that value with a special character like %username% or something like that so you can identify the correct word.

Alternatively if you use “{0}”,“{1}”,“{2}”, etc in your string you can use the Format function to replace them with variables.

For example,

String.Format("My name is {0}",username)

That will replace each number with the sequence of variables.

Hope this helps and there might be other ways too.

Regards.

3 Likes

Hi ClaytonM,

Thank you very much for your reply,

I´m trying to understand how to apply your comment in my code and i´m not sure how to do it.

This is the Datatable Information that i read from an Excel.

I want to do a loop to check if a Text from the “Selector Number” is the same according with the “Form Number” and in case of correct, i would like to type in the “Selector Input” the value of the “Variable” column, but i´m not sure how to assign that for example in the first loop, the Variable Column value will be “aForm585” and i´ve defined a Variable with that exactly name.

I´m not sure how to apply it, could you please help me?

Thanks

You will need to replace using every variable for each row, since you won’t know which value will be on which row.

Let’s put this in psuedocode and maybe it will make better sense:

Read Range

For each row In DT1
    Assign row.Item("Variable") = row.Item("Variable").ToString.Replace("aForm585",aForm585).Replace("aForm20",aForm20).Replace("aForm586",aForm586).Replace("aForm142",aForm142).Trim

EDIT: typo correction

The above will loop through each row and change the value in the “Variable” column to replace the variable.

So you will need to do a replace for every variable name. I do not believe there is a way to turn the variable names into strings in order to compare, so I think you must use many .Replace() functions like I did above for each variable there is.

If there is a better way, I’d be interested too.

Regards.

2 Likes

I Clayton,

Thanks again for your reply, the only weakness that i see is that i need to know all the values in the “Variable” Column.

Thanks!!!

Use a Dictionary(Of String, String) to store your values, not a set of disconnected variables.
Then you can access the value without breaking into data contexts (which btw is possible to access, but it’s extremely brittle to make your implementation rely on that).

So your code could look like this:

// Somewhere in the code
Dictionary(Of String, String) valuesToType = new Dictionary(Of String, String)()
valuesToType("aForm585") = "bar" // instead of aForm585 = "bar"
valuesToType("aForm20") = "baz" // instead of aForm20 = "baz"
...
// The looping part
DataTable someDT = ReadRange(...)
foreach fieldPairToCheck in someDT
If (GetText(selector: fieldPairToCheck("Selector Number")) = fieldPairToCheck("Form Number")
Then SetText(selector: fieldPairToCheck("Selector Input"), value: valuesToType(fieldPairToCheck("Variable")))

Sidenote - I’d change the headers of that table (and a little bit column order).
In current state they don’t tell what they represent and without your paragraph of explanation wouldn’t make sense.
Form Number = 585, 20, 586 etc.
One could get out of that that “Form” is something in the app the robot is working on, and the number is the value of that something.
But:
Selector Number = (some selectors)
Now the first part describes the value, second part describes something in the app (?)
But:
Selector Input
First part is the value, second is something in the app (?)

And Variable doesn’t fir that naming scheme at all.
So the first one is the most inconsistent, but if you switch it around to “Number Form” it doesn’t make much sense either (unless you already know what it represents).

Consider: how could you name them so that your data structure has meaning on it’s own?

If you’d rename/reorder them to something like (I’ll write in the order I’d put them):
Selector Number → FieldToCheckSelector
Form Number → FieldToCheckExpectedValue (or ValueToCheckAgainst ?)
Selector Input → FieldToWriteToSelector
Variable → ValueToWriteKey

You could reason about the data structure on it’s own. Only thing doubtful now is if the value should be written if it matches or not matches, but that could be solved as well.
And even if that’s not solved, if you’ll just have that Excel, you can infere from just the headers:
Robot gets value from FieldToCheck by a selector and checks it’s value and then if (match or not match) writes to second field (which is actually in the next column in the same row, so this could be generalized further) a value that it identifies by a key.

Just from the data structure alone.

Then above pseudo-code becomes:

If (GetText(selector: fieldPairToCheck("FieldToCheckSelector")) = fieldPairToCheck("FieldToCheckExpectedValue ")
Then SetText(selector: fieldPairToCheck("FieldToWriteToSelector"), value: valuesToType(fieldPairToCheck("ValueToWriteKey")))

Which, on it’s own, gives exactly the same message as the data structure in the Excel.

As a side-bonus - if you look at the code and the input structure, you now have an almost-generic workflow to write values from a dictionary, if some other field has expected value.
This could be worked upon further to pass a comparison bool as well (so it could write on match or not match, which could also be stored in the Excel), as well as some other variants (depending on needs).
Which could easily end up allowing an external rule set to exist in that file, as the data structure would naturally read as:
(if) FieldToCheck.Value.CompareTo(ExpectedValue) = MyPredicateBool (then) FieldToWriteTo.Value = ValueToWriteKey // or just ValueToWrite

As Fred Brooks said (and assuming fields are named descriptively):

Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowcharts; they’ll be obvious.

4 Likes

Good suggestion. A dictionary would allow you to use the value from the spreadsheet as the key in the dictionary to access the value that you want to replace it with.

Like to Fred Brooks