Forum

Dynamic plugin. output a string list via pin trouble

hello, having a c# question again. i am trying to build a list inside the plugin and want to output the list via pin.
really not understanding the error i am getting:
“Index was out of range. Must be non-negative and less than the size of the collection.”

the output is 2 slices but identical (the first aaa)
when i search for bbb, i am getting just one empty slice.
can anyone help me out?

- region usings
using System;
using System.ComponentModel.Composition;
using System.Collections.Generic;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using VVVV.Utils.VColor;
using VVVV.Utils.VMath;
- endregion usings

namespace VVVV.Nodes
{
	#region PluginInfo
	[PluginInfo(Name = "Template", Category = "String", Version = "1", Help = "Basic template with one string in/out", Tags = "")](PluginInfo(Name = "Template", Category = "String", Version = "1", Help = "Basic template with one string in/out", Tags = ""))
	#endregion PluginInfo
	public class C1StringTemplateNode : IPluginEvaluate
	{
		#region fields & pins
		[Output("Output")](Output("Output"))
		ISpread<string> FOutput;
		
		[Output("Errors")](Output("Errors"))
		ISpread<string> FErrors;
		#endregion fields & pins
		
		public void Evaluate(int SpreadMax)
		{
			
		try {
			string theString = "aaa 1XXX,bbb XXX,ccc XXX,aaa 2XXX";          
			List<string> listFromTheString = new List<string>(theString.Split(','));           
			List<string> listOfFoundItems = new List<string>();

			for (int i = 0; i < (listFromTheString.Count); i++)
			  {
   			    if(listFromTheString[i](i).Contains("aaa"))
   			    {
       		     listOfFoundItems.Add(listFromTheString[i](i));
    		    			 	
   			     FOutput.SliceCount = listOfFoundItems.Count;
    		     FOutput[i](i) = listOfFoundItems[i](i);
   			  	
   			    } 
    		  }  
		    } catch (Exception error) {FErrors[0](0) = error.Message;}	 

		}
	}
}

ok, the Output has to be in a separate for loop because there can be iterations where there is nothing stored in the list, like when searching for the bbb.

no need for a for loop, the pin has a method .AsignFrom(IEnumerable enumerable) just use this… it does the job for you. it also sets the slicecount of the output pin, which is not done in your code and the actual reason for the error i think…

but does
FOutput.SliceCount = listOfFoundItems.Count;
not set the slicecount? that is how it is working now…
Anyway .AsignFrom seems easier though. thanks

What i think was the problem:
When the for loop wants to iterate 4 times for example (listFromTheString.Count),
but listOfFoundItems has just 1 item, then there will
be an index error.
In the upper case the first iteration is ok because “aaa” is found.
The second will fail because FOutput1 will not have a counterpart of listOfFoundItems1 because listFromTheString1 is “bbb XXX” and not added to the listOfFoundItems.