This content is not available in your preferred language.

The content is shown in another available language. Your browser may include features that can help translate the text.

DIAdem Script: In For Loop, Function Call Using Channel Index Does Not Give Results

Updated May 11, 2023

Reported In

Software

  • DIAdem Full
  • DIAdem Base
  • DIAdem Professional

Issue Details

I have a problem with a script in DIAdem where I try to call a function within a for loop. My function call is similar to this:
 
 
For groupIndex = 1 to 10
Call chnCalculate("ch(""[groupIndex]/channelName"")= ..... ")
Next

I do not get any results if I use the loop counter groupIndex as an index for the group, but my code works as expected when I index the group with a hard coded number. Also, the problem does not only show up when using the Calculate function. Is this a limitation with the code? 

Solution

This problem comes from how the index string is formatted. Here are three different ways to modify your script so that the indexing will be interpreted correctly:
  • Option 1: reformat the call using "& groupIndex &":
For groupIndex = 1 to 10
Call chnCalculate("ch(""["& groupIndex &"]/channelName"")= ..... ") 
Next
  • Option 2: activate the current group, then reference using ch(""/channelName"")​:
For groupIndex = 1 to 10
Data.Root.ChannelGroups(groupIndex).Activate 
Call chnCalculate("ch(""/channelName"")= ..... ") 
Next
  • Option 3: activate the current group, then reference using "& channelName &":
 
For groupIndex = 1 to 10
Data.Root.ChannelGroups(groupIndex).Activate 
Call chnCalculate("ch("""& channelName &""")= ..... ") 
Next
 
Note that you need to add three quotation marks on each since of the & channelName & statement in the last option.
 

Additional Information

  • If you reference the group by name often this could make your code cluttered. Consider creating an object for the active channel with this kind of statement: 
Set oChnGrp = Data.Root.ChannelGroups(groupIndex)
 
  • The 3 solutions to this problem give the same results - pick the one you prefer based on your style of coding.