Call of Variable by using String in a loop

All topics on coding 4Dscript in Enterprise Dynamics.
Post Reply
Roma
Posts: 6
Joined: Monday 08 July, 2013 - 10:54

Call of Variable by using String in a loop

Post by Roma »

Hope you can help me again! Like you did last time!

What I need is a call to a variable, by using a string name in a loop. See for example the (simplified) attached model.

I tried it with: ValueToString, ExecString, ParString, etc. But I cannot find a way to do it.

The main target is to do some calculations with the variable: "tussenstap"

Code: Select all

Do(
 Var([SommatieProduct1] ,vbValue,0  ),
 Var([SommatieProduct2] ,vbValue,0  ),
 Var([SommatieProduct3] ,vbValue,0  ),
 Var([SommatieProduct4] ,vbValue,0  ),
 Var([SommatieProduct5] ,vbValue,0  ),
 
 Var([Runner]           ,vbValue,1  ),

 Var([Samensteller]     ,vbString   ),
 Var([Tussenstap]       ,vbValue    ),
 
 SommatieProduct1:= 0,
 SommatieProduct2:= 4,
 SommatieProduct3:= 8,
 SommatieProduct4:= 12,
 SommatieProduct5:= 16,

For(Runner:=1, Runner < 6, Inc(Runner),
  Do(                 
      Samensteller:= Concat([SommatieProduct],String(Runner)),
     
{And this stept is not working....} 
      Tussenstap := ExecString(Samensteller)
       ) 
    )
  )      
  
menno
Posts: 45
Joined: Tuesday 29 March, 2011 - 16:01

Re: Call of Variable by using String in a loop

Post by menno »

Hello Roma,
ED cannot read a string as a variable.

Maybe you can use labels to achieve this:

Code: Select all

Do(
  Var([SommatieProduct1] ,vbValue,0  ),
  Var([SommatieProduct2] ,vbValue,0  ),
  Var([SommatieProduct3] ,vbValue,0  ),
  Var([SommatieProduct4] ,vbValue,0  ),
  Var([SommatieProduct5] ,vbValue,0  ),
  
  Var([Runner]           ,vbValue,1  ),
 
  Var([Samensteller]     ,vbString   ),
  Var([Tussenstap]       ,vbValue    ),
  
  label([SommatieProduct1], model):= 0,
  label([SommatieProduct2], model):= 4,
  label([SommatieProduct3], model):= 8,
  label([SommatieProduct4], model):= 12,
  label([SommatieProduct5], model):= 16,
 
  For(Runner:=1, Runner < 6, Inc(Runner),
    Do(                 
      Samensteller:= Concat([SommatieProduct],String(Runner)),
      
      {And this stept is not working....}
      Tussenstap := label(Samensteller, Model)

    )
  )
) 
Or use an attribute that you fill with the string 'Samensteller', and subsequently execute the attribute, then you also get the value.

Hopefully this helps!

Kind regards,
Menno
Post Reply