Siemens TIA Portal dereferencing
If you were looking for a way how to pass dynamically assigned variable, you have propably hit a roadblock. You propably tried to make it work with Variants, DB_ANY or referencing. But nothing really worked. So in the end you have used array and you were passing index. What are you really looking for is called dereferencing.
You should also know, requirement for using references is a CPU of the S7-1500 series with the firmware version V2.5 or higher. You can find related informations about this subject in TIA Portal help in category: Programming a PLC > Data types > Pointer > References.
To start let's say, we have defined "User data type" (UDT) with name udt_1. I've added some boolean variables, so we have something to work with.
Next in some DB (in my case DB_1) we have multiple records of this type. Our program will always use only one of the records (udt1, udt2). I've also defined two additional boolean values to control which UDT will be used.
This part will look familiar. We need temporal value (tmp1) with type REF_TO. Then we just need to set reference into it with our control bool values. Setting reference into variable is executed with REF(). Setting reference is like creating link to original variable.
We will need a place where we will pass our temporal value, I've created FC Block_1 with InOut parameter of our UDT. It does some changes with UDT variables, so we can see if it works.
Finally we are going to use dereferencing. We pass our temporal value (tmp1) into this FC with sign ^ as suffix. This will take variable on which is reference pointing and pass it directly as variable.
It's important to call FC only if our variable tmp1 is set. Otherwise this variable will be null and our program will crash.
Now when we start our program and set in our DB "active udt1" to true, variables of first UDT will change by logic defined in FC. Same thing will happen with "active udt2".
One last thing that maybe you have noticed. Example FC set #UDT.A with coil to true and after no longer passing UDT (with setting false into "active udt1/2"), specific <udt>.A stays true. Why? Of course it makes sense because we are no longer passing UDT into FC, so it is no longer connected into that coil and PLC keeps last state for these DB variables. It can be desired or not, it's up to use case. But it's good to remember it.