# Thread: Logic, a loop for an array with type variables

1. User
Join Date
09.04.2015
Posts
17

## Logic, a loop for an array with type variables

Hello,

I have a type BatteryInRack:
Voltage: real;

and a type BatteryRack:
Battery1: BatteryInRack
Battery2: BatteryInRack
Battery3: BatteryInRack
...

I have in Logic a variable:
BatteryRack: BatteryRack

I try to assign a value to the Voltage value, for example:
Code:
`for i:= 1 to 3 doBatteryRack[i]:= i;end_for;`
But it isn't work.

Is there a way to assign the value in a Loop?

Thank you in advance.

2. ## Re: Logic, a loop for an array with type variables

Hi genakust!

Which zenon version do you use?

The way you are trying to implement your battery rack will not work in logic for the following reasons:
1) You do not have an array of BatteryInRack-Variables within your BatteryRack-Structure => using indices won't work
2) Usually index variables are of Integer-based datatypes, so you might need a conversion if the index is used as value
4) Most crucial point: you are trying to assign an Integer-value to a variable of type BatteryInRack => Will never work: how should the BatteryInRack-variablle know which of its sub-variables to take for the value assignment?

I suggest you to modify your project the following way:
1) Replace the single BatteryInRack-variables within BatteryRack-type by a variable array (e.g. Batteries) so you can use the index
2) Rename the BatteryRack-variable to something like MyBatteryRack to avoid name collisions with the same-named datatype
3) Use the length of the array within for-condition, then you are more flexible: for i:=0 to COUNTOF(MyBatteryRack.Batteries)-1 do
4) Target the voltage of the BatteryInRack-variables by BatteryRack.[i].Voltage, e.g. BatteryRack.Batteries[i].Voltage
5) Add an ANY_TO_REAL()- conversion to the value assignment for the voltage, e.g.: BatteryRack.Batteries[i].Voltage = ANY_TO_REAL(i);

I tried it with version 7.60 and it worked in this case.
You find screenshots of the adaptions I made attached.

Hope this helps

BR, klemensn
Last edited by klemensn : 9th May 2018 at 09:31 Reason: Small mistake in the program - out of bounds at top limit of the array, therefore -2

#### Posting Rules

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•